{smcl}
{txt}{sf}{ul off}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}/Users/mlg2296/Dropbox/recount/ReplicationPacket/recount.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}27 Aug 2023, 19:34:43
{txt}
{com}. 
. set more off
{txt}
{com}. 
. *Figure 1
. do do_files`c(dirsep)'Figure1.do
{txt}
{com}. 
.         
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. *NPV Distributions
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. 
. 
. global LIST = "52201 69006 69106 " //M1GerusoEtAl correctedEconomist correctedSilver 
{txt}
{com}. 
. 
. global SD = 100000
{txt}
{com}. global TURNOUT = 150000000
{txt}
{com}. 
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. global STEP = .002      //step along horizontal axis for density plot
{txt}
{com}. global STEP = .004      //step along horizontal axis for density plot
{txt}
{com}. global BINSTART= .42                                    //rightmost point of lowest bin
{txt}
{com}. global BINEND = 1 - $BINSTART + $STEP   //rightmost point of last bin
{txt}
{com}. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define HIST              =J(200, 200, .)         //table for figure: hist
{txt}
{com}. 
. //define bin starts as first row of F
. local row=4
{txt}
{com}. forvalues i=$BINSTART($STEP)$BINEND{c -(} //to hold histogram heights
{txt}  2{com}.         matrix HIST[`row', 1] = `i'
{txt}  3{com}.         local row = `row' + 1
{txt}  4{com}. {c )-}
{txt}
{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 15{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 16{com}.         {c )-}
{txt} 17{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 18{com}.         qui gen pvshare = rvotes/pvotes
{txt} 19{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 20{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 21{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 22{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 23{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 24{com}.         {c )-}
{txt} 25{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 26{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 27{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 28{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 29{com}.         qui gen cf_revn_total  = revn_total
{txt} 30{com}.         qui gen cf_rwin = rwin
{txt} 31{com}. 
.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 32{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 33{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 34{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 35{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim
{txt} 36{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim
{txt} 37{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 38{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 39{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 40{com}.                 label var vmargin_`i' "The margin in vote counts in state `i'"
{txt} 41{com}.                 foreach marg in $MARGLIST{c -(}
{txt} 42{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 43{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 44{com}.                 {c )-}
{txt} 45{com}.         {c )-}
{txt} 46{com}.         
.         gen closest_flippable_state=.
{txt} 47{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 48{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 49{com}.         qui gen tipping_state = .
{txt} 50{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 51{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 52{com}.         {c )-}
{txt} 53{com}. 
.         //EC Reversals
.         egen swingers = rowtotal(swing_*)
{txt} 54{com}.         foreach marg in $MARGLIST{c -(}
{txt} 55{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 56{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 57{com}.                 qui gen notReversed`marg' = swing`marg'==0
{txt} 58{com}.                 label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
{txt} 59{com}.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 60{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 61{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 62{com}.                 
.         {c )-}
{txt} 63{com}.         drop swing`marg'_*
{txt} 64{com}.         
.         //NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 65{com}.                 qui gen PVswing`marg'=0
{txt} 66{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt} 67{com}.                 
.         {c )-}       
{txt} 68{com}. 
.         qui replace swingers = 0 if swingers == .
{txt} 69{com}.         qui gen ec_margin = min_vmargin
{txt} 70{com}.         qui gen pv_margin = margin_votes
{txt} 71{com}.         
.         qui gen zero = swingers == 0
{txt} 72{com}.         label var swingers "number of states that could swing election"
{txt} 73{com}. 
.         //more state level variables
.         forvalues s=1(1)$STATECOUNT{c -(}
{txt} 74{com}.                 //rename whig variables, as needed
.                 cap rename  W_pvShare_`s'  R_pvShare_`s'
{txt} 75{com}.                 //state vote counts for Rs
.                 qui gen r_votes_`s' = R_pvShare_`s'*twoPartyVotes_`s'
{txt} 76{com}.                 //state evs for Rs
.                 qui gen r_ev_`s' = total_ev_`s' if R_pvShare_`s'>=0.50
{txt} 77{com}.                 qui replace r_ev_`s' = 0 if R_pvShare_`s'<0.50
{txt} 78{com}.         
.         {c )-}
{txt} 79{com}.         
.         //buildup string for row totals
.         local votestring = "r_votes_1"
{txt} 80{com}.         local evstring = "r_ev_1"
{txt} 81{com}.         local totalvotesstring = "twoPartyVotes_1"
{txt} 82{com}.         local totalevstring = "total_ev_1"
{txt} 83{com}.         forvalues s=2(1)$STATECOUNT{c -(} 
{txt} 84{com}.                 local votestring = "`votestring' r_votes_`s'"   // R votes row total string
{txt} 85{com}.                 local evstring = "`evstring' r_ev_`s'"                  // R ev row total string
{txt} 86{com}.                 local totalvotesstring = "`totalvotesstring' twoPartyVotes_`s'" //all votes
{txt} 87{com}.                 local totalevstring = "`totalevstring' total_ev_`s'" //all evs
{txt} 88{com}.         {c )-}
{txt} 89{com}.         
.         //sum to national outcomes
.         qui egen r_votes_natl = rowtotal(`votestring')
{txt} 90{com}.         qui egen r_ev_natl = rowtotal(`evstring')
{txt} 91{com}.         qui egen totalvotes_natl = rowtotal(`totalvotesstring')
{txt} 92{com}.         qui egen totalev_natl = rowtotal(`totalevstring')
{txt} 93{com}.         
.         qui gen cutoff = floor(totalev_natl/2) + 1 
{txt} 94{com}.         qui gen r_share_natl = r_votes_natl/totalvotes_natl
{txt} 95{com}.         qui gen r_ev_win = (r_ev_natl >= cutoff)
{txt} 96{com}.         qui gen r_pv_win = (r_votes_natl>=(totalvotes_natl/2))
{txt} 97{com}.         qui gen inv = r_ev_win!=r_pv_win 
{txt} 98{com}.         label var r_share_natl "R share nationally"
{txt} 99{com}.         label var r_ev_win "R ev win nationally"
{txt}100{com}.         label var r_pv_win "R pv win nationally"
{txt}101{com}.         label var inv "inversion"
{txt}102{com}. 
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt}103{com}.         di `col'
{txt}104{com}.         di `file'
{txt}105{com}.         matrix REC[1, `col']            = `file'
{txt}106{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt}107{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt}108{com}.         matrix RPV[1, `col']            = `file'
{txt}109{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}110{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}111{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}112{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}113{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}114{com}.                 matrix RPV[`row',1] = `marg'
{txt}115{com}.                 
.                 qui summ Reversed`marg'
{txt}116{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}117{com}.                 qui summ Reversed`marg' if dwin==1
{txt}118{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}119{com}.                 qui summ Reversed`marg' if rwin==1
{txt}120{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}121{com}.                 
.                 qui summ PVswing`marg'
{txt}122{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}123{com}.                 
.                 local row = `row'+1
{txt}124{com}.         {c )-}
{txt}125{com}.         
.         matrix list REC
{txt}126{com}.         
.         ************************************************
.         *Data for density figures
.         ************************************************
.         local col = $LOOP
{txt}127{com}.         
.         matrix HIST[1,`col']    = `file'
{txt}128{com}. 
.         local row = 4
{txt}129{com}.         forvalues i= $BINSTART($STEP)$BINEND {c -(}
{txt}130{com}.                 local last = `i'-$STEP
{txt}131{com}.                 local next = `i'
{txt}132{com}.                 matrix HIST[`row', 1] = `i'- $STEP/2    // first column for horizontal axis
{txt}133{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' 
{txt}134{com}.                 local den = r(N)
{txt}135{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' & r_ev_win==1 
{txt}136{com}.                 local num = r(N)        
{txt}137{com}.                 local win = `num'/`den'
{txt}138{com}.                 matrix HIST[`row',`col'] = `den'
{txt}139{com}.                 local row = `row' + 1
{txt}140{com}.         {c )-}
{txt}141{com}. 
. ************************************************
. *Outer loop - close datasets/convert matrices
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}142{com}. di "$LOOP"
{txt}143{com}.         
. {c )-}
51
{txt}(100,000 missing values generated)
2
52201
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201       .       .       .       .       .       .       .       .
{txt} r2 {res}     50  .00022       .       .       .       .       .       .       .       .
{txt} r3 {res}     75  .00036       .       .       .       .       .       .       .       .
{txt} r4 {res}    100  .00047       .       .       .       .       .       .       .       .
{txt} r5 {res}    150  .00073       .       .       .       .       .       .       .       .
{txt} r6 {res}    225  .00112       .       .       .       .       .       .       .       .
{txt} r7 {res}    330  .00162       .       .       .       .       .       .       .       .
{txt} r8 {res}    500  .00237       .       .       .       .       .       .       .       .
{txt} r9 {res}    750  .00335       .       .       .       .       .       .       .       .
{txt}r10 {res}   1000  .00466       .       .       .       .       .       .       .       .
{txt}r11 {res}   1500  .00678       .       .       .       .       .       .       .       .
{txt}r12 {res}   2250     .01       .       .       .       .       .       .       .       .
{txt}r13 {res}   3300  .01436       .       .       .       .       .       .       .       .
{txt}r14 {res}   5000  .02153       .       .       .       .       .       .       .       .
{txt}r15 {res}   7500  .03137       .       .       .       .       .       .       .       .
{txt}r16 {res}  10000  .04034       .       .       .       .       .       .       .       .
{txt}r17 {res}  15000  .05617       .       .       .       .       .       .       .       .
{txt}r18 {res}  22500   .0747       .       .       .       .       .       .       .       .
{txt}r19 {res}  33000  .09467       .       .       .       .       .       .       .       .
{txt}r20 {res}  50000  .11716       .       .       .       .       .       .       .       .
{txt}r21 {res}  75000  .14006       .       .       .       .       .       .       .       .
{txt}r22 {res} 100000  .15675       .       .       .       .       .       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}3
51
(40,000 missing values generated)
3
69006
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    52201    69006        .        .        .        .        .        .
{txt} r2 {res}      50   .00022   .00175        .        .        .        .        .        .
{txt} r3 {res}      75   .00036   .00175        .        .        .        .        .        .
{txt} r4 {res}     100   .00047   .00175        .        .        .        .        .        .
{txt} r5 {res}     150   .00073  .001775        .        .        .        .        .        .
{txt} r6 {res}     225   .00112    .0019        .        .        .        .        .        .
{txt} r7 {res}     330   .00162  .002125        .        .        .        .        .        .
{txt} r8 {res}     500   .00237    .0024        .        .        .        .        .        .
{txt} r9 {res}     750   .00335  .003475        .        .        .        .        .        .
{txt}r10 {res}    1000   .00466  .004875        .        .        .        .        .        .
{txt}r11 {res}    1500   .00678    .0072        .        .        .        .        .        .
{txt}r12 {res}    2250      .01    .0098        .        .        .        .        .        .
{txt}r13 {res}    3300   .01436  .013925        .        .        .        .        .        .
{txt}r14 {res}    5000   .02153  .021525        .        .        .        .        .        .
{txt}r15 {res}    7500   .03137  .030925        .        .        .        .        .        .
{txt}r16 {res}   10000   .04034    .0407        .        .        .        .        .        .
{txt}r17 {res}   15000   .05617   .05715        .        .        .        .        .        .
{txt}r18 {res}   22500    .0747   .07965        .        .        .        .        .        .
{txt}r19 {res}   33000   .09467  .104475        .        .        .        .        .        .
{txt}r20 {res}   50000   .11716   .13705        .        .        .        .        .        .
{txt}r21 {res}   75000   .14006    .1684        .        .        .        .        .        .
{txt}r22 {res}  100000   .15675  .189875        .        .        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}4
51
(40,000 missing values generated)
4
69106
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    52201    69006    69106        .        .        .        .        .
{txt} r2 {res}      50   .00022   .00175  .000725        .        .        .        .        .
{txt} r3 {res}      75   .00036   .00175  .000725        .        .        .        .        .
{txt} r4 {res}     100   .00047   .00175  .000725        .        .        .        .        .
{txt} r5 {res}     150   .00073  .001775   .00095        .        .        .        .        .
{txt} r6 {res}     225   .00112    .0019  .000975        .        .        .        .        .
{txt} r7 {res}     330   .00162  .002125  .001675        .        .        .        .        .
{txt} r8 {res}     500   .00237    .0024    .0024        .        .        .        .        .
{txt} r9 {res}     750   .00335  .003475  .003875        .        .        .        .        .
{txt}r10 {res}    1000   .00466  .004875  .004975        .        .        .        .        .
{txt}r11 {res}    1500   .00678    .0072  .007975        .        .        .        .        .
{txt}r12 {res}    2250      .01    .0098  .011475        .        .        .        .        .
{txt}r13 {res}    3300   .01436  .013925   .01625        .        .        .        .        .
{txt}r14 {res}    5000   .02153  .021525  .024225        .        .        .        .        .
{txt}r15 {res}    7500   .03137  .030925   .03365        .        .        .        .        .
{txt}r16 {res}   10000   .04034    .0407    .0437        .        .        .        .        .
{txt}r17 {res}   15000   .05617   .05715   .06215        .        .        .        .        .
{txt}r18 {res}   22500    .0747   .07965  .084325        .        .        .        .        .
{txt}r19 {res}   33000   .09467  .104475  .110125        .        .        .        .        .
{txt}r20 {res}   50000   .11716   .13705  .142575        .        .        .        .        .
{txt}r21 {res}   75000   .14006    .1684  .174475        .        .        .        .        .
{txt}r22 {res}  100000   .15675  .189875  .199475        .        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}5

{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. cap drop REC*
{txt}
{com}. svmat REC
{txt}
{com}. cap drop RPV*
{txt}
{com}. svmat RPV
{txt}
{com}. cap drop HIST*
{txt}
{com}. svmat HIST
{txt}
{com}. 
. 
. //align the bar heights of histograms, given the 100k v 40k sim runs
. replace HIST2=HIST2*4/10
{txt}(43 real changes made)

{com}. 
. ************************************************
. *Plot
. ************************************************
. 
. 
. local labsize = "small"
{txt}
{com}. twoway ///
>         (connected HIST2 HIST1 in 2/100 , lw(medium) lcolor(black) ms(none) ) ///
>         (connected HIST3 HIST1 in 2/100 , lw(medium)  lpattern(longdash) lcolor(black) ms(none) ) ///
>         (connected HIST4 HIST1 in 2/100 , lw(thin) lpattern(shortdash) lcolor(black) ms(none) ) ///
>         , ///
>         aspect(.5) ///
>         ylabel(0 " ", nogrid labsize(medium) axis(1)) ///
>         xlabel(.42(.02).58, nogrid labsize(`labsize')) ///
>         xline(.5, lcolor(gs11) lpattern(dash) ) ///
>         xtitle("Republican Share of NPV", size(`labsize')) ///
>         ytitle("Probability", size(`labsize') axis(1)) ///
>         graphr(c(white) lc(white)) ///
>         legend(order(2 "Gelman et al., 2020" 1 "Geruso et al., 2022"  3 "Silver, 2020") position(2) col(1) ring(0) region(lwidth(thin)) size(small))  ///
>         name(histoverlay_1, replace) 
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'Figure1a.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/Figure1a.pdf written in PDF format)

{com}.         
.         
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. *ECV Distributions
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"
{txt}
{com}. 
. global LIST = "52201 69006 69106 " // M1 correctedEconomist correctedSilver 
{txt}
{com}. 
. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
.         
. ************************************************
. *Make vars
. ************************************************        
. 
.         if `file'==52201{c -(}
{txt}  4{com}.                 local simlabel = "Geruso et al., 2022"
{txt}  5{com}.                 local ycoord = .002
{txt}  6{com}.         {c )-}
{txt}  7{com}.         if `file'==69006{c -(}
{txt}  8{com}.                 local simlabel = "Gelman et al., 2020"
{txt}  9{com}.                 local ycoord = .005
{txt} 10{com}.         {c )-}
{txt} 11{com}.         if `file'==69106{c -(}
{txt} 12{com}.                 local simlabel = "Silver, 2020"
{txt} 13{com}.                 local ycoord = .005
{txt} 14{com}.         {c )-}
{txt} 15{com}.         
.         //count states 
.         local error=0
{txt} 16{com}.         local counter=1
{txt} 17{com}.         while `error' != 111 {c -(}
{txt} 18{com}.                 qui cap d total_ev_`counter'
{txt} 19{com}.                 local error=_rc
{txt} 20{com}.                 local counter=`counter'+1
{txt} 21{com}.         {c )-}
{txt} 22{com}.         global STATECOUNT = `counter'-2 
{txt} 23{com}.         di "$STATECOUNT"
{txt} 24{com}.         local state_count = `counter'-2 
{txt} 25{com}. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 26{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 27{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 28{com}.         {c )-}
{txt} 29{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 30{com}.         qui gen pvshare = rvotes/pvotes
{txt} 31{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 32{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 33{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 34{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 35{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 36{com}.         {c )-}
{txt} 37{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 38{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 39{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 40{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 41{com}.         qui gen cf_revn_total  = revn_total
{txt} 42{com}.         qui gen cf_rwin = rwin
{txt} 43{com}. 
.         qui gen inv=0
{txt} 44{com}.         qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)
{txt} 45{com}. 
.         
.         local labsize = "vlarge"
{txt} 46{com}.         hist revn_total  , ///
>                 xtitle("Republican EC Votes", size(`labsize')) xlabel(0 100  270 400 538, nogrid labsize(`labsize')) ///
>                 bfcolor(gs8) blcolor(gs8) ///   
>                 graphregion(color(white)) plotregion(color(white)) ///
>                 xsc( r(0 538)) ///
>                 start(0) fysize(25) width(5) barwidth(3.5) ///
>                 ylabel(none) xline(270, lcolor(gs11) lpattern(dash) ) ///               
>                 ytitle("") ///
>                 text(`ycoord' 400 "`simlabel'" , color(gs8) place(e) justification(left) size(`labsize')) ///
>                 name(hist_`file', replace) saving(figures`c(dirsep)'hist_`file', replace)       
{txt} 47{com}. 
.                 *graph export "figures`c(dirsep)'Main_modern.pdf", replace      
.         
. ************************************************
. *Outer loop
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt} 48{com}. di "$LOOP"
{txt} 49{com}.         
. {c )-}
51
{txt}(bin={res}106{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_52201.gph not found)
{res}{txt}(file figures/hist_52201.gph saved)
3
51
(bin={res}78{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_69006.gph not found)
{res}{txt}(file figures/hist_69006.gph saved)
4
51
(bin={res}101{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_69106.gph not found)
{res}{txt}(file figures/hist_69106.gph saved)
5

{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. ************************************************
. *Figures
. ************************************************
. 
. graph combine "figures`c(dirsep)'hist_69006.gph" "figures`c(dirsep)'hist_52201.gph"  "figures`c(dirsep)'hist_69106.gph", ///
>                 graphregion(color(white)) rows(3) cols(1) xsize(10) ysize(5) xcommon
{res}{txt}
{com}.                 
. graph export "figures`c(dirsep)'Figure1b.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/Figure1b.pdf written in PDF format)

{com}. 
. 
{txt}end of do-file

{com}. 
. *Figure 2, Figure A1, Panel A of Table 2 
. do do_files`c(dirsep)'Figure2Table2aFigureA1.do
{txt}
{com}. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"
{txt}
{com}. 
. global LIST = "52201 69001 69101 69006 69106 69201" //M1GerusoEtAl Economist Silver correctedEconomist correctedSilver GSE2010
{txt}
{com}. 
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates;                                           columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECPCT    =J(30, 10, .)           //table of summary of reversal rates based on % margins;        columns are models, rows are %critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on R EC WINS
{txt}
{com}. matrix define RECRECFRACWIN     =J(30, 10, .)           //table of summary of reversal rates; condl frac of R EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define RPVPCT    =J(30, 10, .)           //table of summary of NPV reversal rates; in % margins (pp)
{txt}
{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 15{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 16{com}.         {c )-}
{txt} 17{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 18{com}.         qui gen pvshare = rvotes/pvotes
{txt} 19{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 20{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 21{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 22{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 23{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 24{com}.         {c )-}
{txt} 25{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 26{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 27{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 28{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 29{com}.         qui gen cf_revn_total  = revn_total
{txt} 30{com}.         qui gen cf_rwin = rwin
{txt} 31{com}. 
.         qui gen inv=0
{txt} 32{com}.         qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)
{txt} 33{com}.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 34{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 35{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 36{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 37{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim (nationally)
{txt} 38{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim (nationally)
{txt} 39{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 40{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 41{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 42{com}.                 label var vmargin_`i' "The margin in vote counts in state `i', if `i' could flip EC"
{txt} 43{com}.                 
.                 //Levels: cylce through marglist to get probability of flips at each margin
.                 foreach marg in $MARGLIST{c -(}
{txt} 44{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 45{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 46{com}.                 {c )-}
{txt} 47{com}.                 //PP: cylce through % marglist to get probability of flips at each margin
.                 foreach marg in $MARGLISTPCT{c -(}
{txt} 48{com}.                         local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 49{com}.                         qui gen swingpct`labelmarg'_`i' = swing_`i'==1 & (vmargin_`i'/pvote)<`marg'
{txt} 50{com}.                         label var swingpct`labelmarg'_`i' "Binary: is vmargin_`i' less than `marg' expressed as pct of turnout"
{txt} 51{com}.                 {c )-}       
{txt} 52{com}.                 
.         {c )-}
{txt} 53{com}.         
.         qui gen closest_flippable_state=.
{txt} 54{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 55{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 56{com}.         qui gen tipping_state = .
{txt} 57{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 58{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 59{com}.         {c )-}
{txt} 60{com}.         egen swingers = rowtotal(swing_*)
{txt} 61{com}.         
.         //Levels EC Reversals 
.         foreach marg in $MARGLIST{c -(}
{txt} 62{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 63{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 64{com}.                 *qui gen notReversed`marg' = swing`marg'==0
.                 *label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 65{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 66{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 67{com}.         {c )-}
{txt} 68{com}.                 
.         //PP EC Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt} 69{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 70{com}.                 di `labelmarg'
{txt} 71{com}.                 qui egen swingpct`labelmarg' = rowtotal(swingpct`labelmarg'_*)
{txt} 72{com}.                 qui replace swingpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 73{com}.                 *qui gen notReversed`marg' = swing`marg'==0
.                 *label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
.                 qui gen Reversedpct`labelmarg' = (swingpct`labelmarg'>0 & swingpct`labelmarg'<51)
{txt} 74{com}.                 qui replace Reversedpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 75{com}.                 label var Reversedpct`labelmarg' "EC reversed by at least one state dropping `marg' pp votes"
{txt} 76{com}.         {c )-}
{txt} 77{com}.         
.         //Levels NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 78{com}.                 qui gen PVswing`marg'=0
{txt} 79{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt} 80{com}.         {c )-}
{txt} 81{com}.         //PP NPV Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt} 82{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 83{com}.                 qui gen PVswingpct`labelmarg'=0
{txt} 84{com}.                 qui replace PVswingpct`labelmarg' = 1 if (margin_votes/pvotes)<`marg'
{txt} 85{com}.         {c )-}       
{txt} 86{com}. 
.         qui replace swingers = 0 if swingers == .
{txt} 87{com}.         qui gen ec_margin = min_vmargin
{txt} 88{com}.         qui gen pv_margin = margin_votes
{txt} 89{com}.         
.         qui gen zero = swingers == 0
{txt} 90{com}.         label var swingers "number of states that could swing election"
{txt} 91{com}.                 
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt} 92{com}.         di `col'
{txt} 93{com}.         di `file'
{txt} 94{com}.         matrix REC[1, `col']            = `file'
{txt} 95{com}.         matrix RECPCT[1, `col']         = `file'
{txt} 96{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt} 97{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt} 98{com}.         matrix RECRECFRACWIN[1, `col']  = `file'        
{txt} 99{com}.         matrix RPV[1, `col']            = `file'
{txt}100{com}.         matrix RPVPCT[1, `col']         = `file'        
{txt}101{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}102{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}103{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}104{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}105{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}106{com}.                 matrix RECRECFRACWIN[`row',1] = `marg'
{txt}107{com}.                 matrix RPV[`row',1] = `marg'
{txt}108{com}.                 
.                 qui summ Reversed`marg'
{txt}109{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}110{com}.                 
.                 qui summ Reversed`marg' if dwin==1
{txt}111{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}112{com}.                 qui count if Reversed`marg'==1 & dwin==1        
{txt}113{com}.                 local decwin = r(N)
{txt}114{com}.                 
.                 qui summ Reversed`marg' if rwin==1
{txt}115{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}116{com}.                 qui count if Reversed`marg'==1 & rwin==1
{txt}117{com}.                 local recwin = r(N)
{txt}118{com}.                 
.                 matrix RECRECFRACWIN[`row',`col']=`recwin'/(`recwin'+`decwin')
{txt}119{com}.                 
.                 qui summ PVswing`marg'
{txt}120{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}121{com}.                 
.                 local row = `row'+1
{txt}122{com}.         {c )-}
{txt}123{com}.         
.         local row = 2 //place marglist in rows starting at 2
{txt}124{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLISTPCT{c -(}
{txt}125{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt}126{com}.                 matrix RECPCT[`row',1] = `marg'
{txt}127{com}.                 matrix RPVPCT[`row',1] = `marg'
{txt}128{com}.                 
.                 qui summ Reversedpct`labelmarg'
{txt}129{com}.                 matrix RECPCT[`row',`col'] = r(mean)
{txt}130{com}.                 qui summ PVswingpct`labelmarg'
{txt}131{com}.                 matrix RPVPCT[`row',`col'] = r(mean)            
{txt}132{com}.                 
.                 local row = `row'+1
{txt}133{com}.         {c )-}       
{txt}134{com}.         
. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}135{com}. di "$LOOP"
{txt}136{com}.         
. {c )-}
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
2
52201
3
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
3
69001
4
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
4
69101
5
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
5
69006
6
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
6
69106
7
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
7
69201
8
{txt}
{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. cap drop REC*
{txt}
{com}. svmat REC
{txt}
{com}. svmat RECDECWIN
{txt}
{com}. svmat RECRECWIN
{txt}
{com}. svmat RECRECFRACWIN
{txt}
{com}. cap drop RPV*
{txt}
{com}. svmat RPV
{txt}
{com}. cap drop RECPCT*
{txt}
{com}. svmat RECPCT
{txt}
{com}. cap drop RPVPCT*
{txt}
{com}. svmat RPVPCT
{txt}
{com}. 
. 
. ************************************************
. *Plots
. ************************************************
. 
. qui summ RECPCT2 in 3 
{txt}
{com}. local ecpostY = r(mean) + .0005
{txt}
{com}. qui summ RECPCT1 in 3
{txt}
{com}. local ecpostX = r(mean)
{txt}
{com}. 
. qui summ RPVPCT2 in 14 
{txt}
{com}. local npvpostY = r(mean) - .0127
{txt}
{com}. qui summ RECPCT1 in 14 
{txt}
{com}. local npvpostX = r(mean) - .0005
{txt}
{com}.         
. local labsize = "medsmall"
{txt}
{com}. local legsize = "small"
{txt}
{com}. twoway ///
>         (connected RECPCT2 RECPCT1 in 2/100 , lw(medium) lcolor(black) ms(none) ) ///
>         (connected RECPCT5 RECPCT1 in 6/100 , lw(medium)  lpattern(longdash) lcolor(black) ms(none) ) ///
>         (connected RECPCT6 RECPCT1 in 6/100 , lw(medium) lpattern(shortdash) lcolor(black) ms(none) ) ///
>         (connected RPVPCT2 RECPCT1 in 2/100 if RPVPCT2>0.00001, lw(medium) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT5 RECPCT1 in 6/100 if RPVPCT5>0.00001, lw(medium) lpattern(longdash) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT6 RECPCT1 in 6/100 if RPVPCT6>0.00001, lw(medium) lpattern(shortdash) lcolor(gs11) ms(none) ) ///
>         , ///
>         aspect(1) ///
>         xtitle("Margin (Percent of Turnout)", size(`labsize')) ///
>         ytitle("Probability Margin Is Pivotal", size(`labsize')) ///
>         ysc(log r(0.00005 0.25)) ///
>         xsc(log r(.00001 .001)) ///
>         ylab(0.0001 ".01%" 0.0005 ".05%" 0.0010 ".10%" 0.0050 "0.5%" 0.0100 "1.0%" 0.0500 "5.0%" 0.1000 "10.0%" , nogrid angle(horizontal) labsize(`labsize')) ///
>         xlab(.0000001 `"".00001%" "15 votes""' .000001 `"".0001%" "150""'  .00001 `"".001%" "1,500""'  0.0001 `"".01%" "15,000""'  0.001 `"".1%" "150,000""' , nogrid angle(horizontal) labsize(`labsize') ) ///
>         graphr(c(white) lc(white)) ///
>         name(Modern, replace) ///
>         legend(order(2 "Gelman et al., 2020" 1 "Geruso et al., 2022"  3 "Silver, 2020") position(10) col(1) ring(0) region(lwidth(thin)) size(`legsize'))  ///
>         text(`ecpostY' `ecpostX' "EC", color(black) size(`labsize')) text(`npvpostY' `npvpostX' "Pop. Vote", color(gs11) size(`labsize'))
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'Figure2.pdf", replace   
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/Figure2.pdf written in PDF format)

{com}.         
. 
.         
. qui summ RECPCT2 in 3 
{txt}
{com}. local ecpostY = r(mean) + .0005
{txt}
{com}. qui summ RECPCT1 in 3
{txt}
{com}. local ecpostX = r(mean)
{txt}
{com}. 
. qui summ RPVPCT2 in 14 
{txt}
{com}. local npvpostY = r(mean) - .0127
{txt}
{com}. qui summ RECPCT1 in 14
{txt}
{com}. local npvpostX = r(mean) - .0005
{txt}
{com}.         
. local labsize = "med"
{txt}
{com}. local legsize = "small"
{txt}
{com}. twoway ///
>         (connected RECPCT2 RECPCT1 in 2/100 , lw(medium) lcolor(black) ms(none) ) ///
>         (connected RECPCT3 RECPCT1 in 5/100 , lw(thick)  lpattern(..-) lcolor(black) ms(none) ) ///
>         (connected RECPCT4 RECPCT1 in 4/100 , lw(thin) lpattern(--.) lcolor(black) ms(none) ) ///
>         (connected RECPCT5 RECPCT1 in 5/100 , lw(medium)  lpattern(longdash) lcolor(black) ms(none) ) ///
>         (connected RECPCT6 RECPCT1 in 4/100 , lw(medium) lpattern(shortdash) lcolor(black) ms(none) ) ///
>         (connected RECPCT7 RECPCT1 in 4/100 , lw(medium) lpattern(dash) lcolor(black) ms(none) ) ///
>         (connected RPVPCT2 RECPCT1 in 2/100 if RPVPCT2>0.00001, lw(medium) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT3 RECPCT1 in 2/100 if RPVPCT3>0.00001, lw(thick)  lpattern(..-) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT4 RECPCT1 in 2/100 if RPVPCT4>0.00001, lw(thin) lpattern(--.) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT5 RECPCT1 in 2/100 if RPVPCT5>0.00001, lw(medium)  lpattern(longdash) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT6 RECPCT1 in 2/100 if RPVPCT6>0.00001, lw(medium) lpattern(shortdash) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT7 RECPCT1 in 4/100 if RPVPCT7>0.00001, lw(medium) lpattern(dash) lcolor(gs11) ms(none) ) ///
>         , ///
>         aspect(1) ///
>         xtitle("Margin (Percent of Turnout)", size(`labsize')) ///
>         ytitle("Probability Margin Is Pivotal", size(`labsize')) ///
>         ysc(log r(0.00005 0.25)) ///
>         xsc(log r(.00001 .001)) ///
>         ylab(0.0001 ".01%" 0.0005 ".05%" 0.0010 ".10%" 0.0050 "0.5%" 0.0100 "1.0%" 0.0500 "5.0%" 0.1000 "10.0%" , nogrid angle(horizontal) labsize(`labsize')) ///
>         xlab(.0000001 `"".00001%" "15""' .000001 `"".0001%" "150""'  .00001 `"".001%" "1,500""'  0.0001 `"".01%" "15,000""'  0.001 `"".1%" "150,000""' , nogrid angle(horizontal) labsize(`labsize') ) ///
>         graphr(c(white) lc(white)) ///
>         name(Modern_extended_legend, replace) ///
>         legend(order(2 "Gelman et al., 2020 (uncorrected)" 4 "Gelman et al., 2020 (corrected)"  3 "Silver, 2020 (uncorrected)" 5 "Silver, 2020 (corrected)" 1 "Geruso et al., 2022" 6 "Gelman, Silver, Edlin, 2012") col(1) size(`legsize'))  ///
>         text(`ecpostY' `ecpostX' "EC", color(black)) text(`npvpostY' `npvpostX' "Pop. Vote", color(gs11))
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'FigureA1a_legend.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA1a_legend.pdf written in PDF format)

{com}.         
.         
. twoway ///
>         (connected RECPCT2 RECPCT1 in 2/100 , lw(medium) lcolor(black) ms(none) ) ///
>         (connected RECPCT3 RECPCT1 in 5/100 , lw(thick)  lpattern(..-) lcolor(black) ms(none) ) ///
>         (connected RECPCT4 RECPCT1 in 4/100 , lw(thin) lpattern(--.) lcolor(black) ms(none) ) ///
>         (connected RECPCT5 RECPCT1 in 5/100 , lw(medium)  lpattern(longdash) lcolor(black) ms(none) ) ///
>         (connected RECPCT6 RECPCT1 in 4/100 , lw(medium) lpattern(shortdash) lcolor(black) ms(none) ) ///
>         (connected RECPCT7 RECPCT1 in 4/100 , lw(medium) lpattern(dash) lcolor(black) ms(none) ) ///
>         (connected RPVPCT2 RECPCT1 in 2/100 if RPVPCT2>0.00001, lw(medium) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT3 RECPCT1 in 2/100 if RPVPCT3>0.00001, lw(thick)  lpattern(..-) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT4 RECPCT1 in 2/100 if RPVPCT4>0.00001, lw(thin) lpattern(--.) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT5 RECPCT1 in 2/100 if RPVPCT5>0.00001, lw(medium)  lpattern(longdash) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT6 RECPCT1 in 2/100 if RPVPCT6>0.00001, lw(medium) lpattern(shortdash) lcolor(gs11) ms(none) ) ///
>         (connected RPVPCT7 RECPCT1 in 4/100 if RPVPCT7>0.00001, lw(medium) lpattern(dash) lcolor(gs11) ms(none) ) ///
>         , ///
>         aspect(1) ///
>         xtitle("Margin (Percent of Turnout)", size(`labsize')) ///
>         ytitle("Probability Margin Is Pivotal", size(`labsize')) ///
>         ysc(log r(0.00005 0.25)) ///
>         xsc(log r(.00001 .001)) ///
>         ylab(0.0001 ".01%" 0.0005 ".05%" 0.0010 ".10%" 0.0050 "0.5%" 0.0100 "1.0%" 0.0500 "5.0%" 0.1000 "10.0%" , nogrid angle(horizontal) labsize(`labsize')) ///
>         xlab(.0000001 `"".00001%" "15 votes""' .000001 `"".0001%" "150""'  .00001 `"".001%" "1,500""'  0.0001 `"".01%" "15,000""'  0.001 `"".1%" "150,000""' , nogrid angle(horizontal) labsize(`labsize') ) ///
>         graphr(c(white) lc(white)) ///
>         name(Modern_extended, replace) ///
>         legend(off)  ///
>         text(`ecpostY' `ecpostX' "EC", color(black)) text(`npvpostY' `npvpostX' "Pop. Vote", color(gs11))
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'FigureA1a.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA1a.pdf written in PDF format)

{com}. 
.         
. ************************************************
. *Reference Tables
. ************************************************        
. matrix list RECPCT
{res}
{txt}RECPCT[30,10]
            c1         c2         c3         c4         c5         c6         c7
 r1 {res}         .      52201      69001      69101      69006      69106      69201
{txt} r2 {res} 1.000e-07     .00009    .000575          0     .00175    .000725      .0014
{txt} r3 {res} 2.000e-07     .00017    .000575    .000075     .00175    .000725      .0014
{txt} r4 {res} 5.000e-07     .00033    .000575    .000225     .00175    .000725      .0014
{txt} r5 {res} 1.000e-06     .00066    .000575    .000425     .00175     .00095      .0014
{txt} r6 {res} 2.000e-06     .00136     .00065     .00055     .00195    .001325      .0014
{txt} r7 {res} 5.000e-06     .00314       .001     .00165      .0034      .0037      .0015
{txt} r8 {res}    .00001     .00628      .0017    .003175     .00705     .00735      .0023
{txt} r9 {res}    .00002     .01208      .0029    .005725    .011975      .0139      .0036
{txt}r10 {res}    .00005     .02889     .00735     .01425    .028025    .031575      .0087
{txt}r11 {res}     .0001     .05251    .013325    .026375    .053475      .0577       .016
{txt}r12 {res}     .0002     .08439    .023225    .043825    .092075    .096675      .0267
{txt}r13 {res}     .0005     .13472    .039725      .0758      .1616     .16705      .0421
{txt}r14 {res}      .001     .17543     .05265      .1027     .21135    .226125      .0544
{txt}r15 {res}         .          .          .          .          .          .          .
{txt}r16 {res}         .          .          .          .          .          .          .
{txt}r17 {res}         .          .          .          .          .          .          .
{txt}r18 {res}         .          .          .          .          .          .          .
{txt}r19 {res}         .          .          .          .          .          .          .
{txt}r20 {res}         .          .          .          .          .          .          .
{txt}r21 {res}         .          .          .          .          .          .          .
{txt}r22 {res}         .          .          .          .          .          .          .
{txt}r23 {res}         .          .          .          .          .          .          .
{txt}r24 {res}         .          .          .          .          .          .          .
{txt}r25 {res}         .          .          .          .          .          .          .
{txt}r26 {res}         .          .          .          .          .          .          .
{txt}r27 {res}         .          .          .          .          .          .          .
{txt}r28 {res}         .          .          .          .          .          .          .
{txt}r29 {res}         .          .          .          .          .          .          .
{txt}r30 {res}         .          .          .          .          .          .          .

{txt}            c8         c9        c10
 r1 {res}         .          .          .
{txt} r2 {res}         .          .          .
{txt} r3 {res}         .          .          .
{txt} r4 {res}         .          .          .
{txt} r5 {res}         .          .          .
{txt} r6 {res}         .          .          .
{txt} r7 {res}         .          .          .
{txt} r8 {res}         .          .          .
{txt} r9 {res}         .          .          .
{txt}r10 {res}         .          .          .
{txt}r11 {res}         .          .          .
{txt}r12 {res}         .          .          .
{txt}r13 {res}         .          .          .
{txt}r14 {res}         .          .          .
{txt}r15 {res}         .          .          .
{txt}r16 {res}         .          .          .
{txt}r17 {res}         .          .          .
{txt}r18 {res}         .          .          .
{txt}r19 {res}         .          .          .
{txt}r20 {res}         .          .          .
{txt}r21 {res}         .          .          .
{txt}r22 {res}         .          .          .
{txt}r23 {res}         .          .          .
{txt}r24 {res}         .          .          .
{txt}r25 {res}         .          .          .
{txt}r26 {res}         .          .          .
{txt}r27 {res}         .          .          .
{txt}r28 {res}         .          .          .
{txt}r29 {res}         .          .          .
{txt}r30 {res}         .          .          .
{reset}
{com}. 
. matrix col1     =RECPCT[2..15,1]
{txt}
{com}. matrix col2 = col1*150000000
{txt}
{com}. matrix col3     =RECPCT[2..15,5]
{txt}
{com}. matrix col4     =RECPCT[2..15,2]
{txt}
{com}. matrix col5     =RECPCT[2..15,6]
{txt}
{com}. 
. //binding low resolution in Silver, Gelman due to truncation of source data
. forvalues i=1(1)4{c -(}
{txt}  2{com}.         matrix col5[`i',1]=.
{txt}  3{com}.         matrix col3[`i',1]=.
{txt}  4{com}. {c )-}
{txt}
{com}. matrix PANELA = (col1,col2,col3,col4,col5)
{txt}
{com}. matrix colnames PANELA = percent votesif150M Gelman Geruso Silver
{txt}
{com}. matrix list PANELA
{res}
{txt}PANELA[14,5]
         percent  votesif150M       Gelman       Geruso       Silver
 r2 {res}   1.000e-07           15            .       .00009            .
{txt} r3 {res}   2.000e-07           30            .       .00017            .
{txt} r4 {res}   5.000e-07           75            .       .00033            .
{txt} r5 {res}   1.000e-06          150            .       .00066            .
{txt} r6 {res}   2.000e-06          300       .00195       .00136      .001325
{txt} r7 {res}   5.000e-06          750        .0034       .00314        .0037
{txt} r8 {res}      .00001         1500       .00705       .00628       .00735
{txt} r9 {res}      .00002         3000      .011975       .01208        .0139
{txt}r10 {res}      .00005         7500      .028025       .02889      .031575
{txt}r11 {res}       .0001        15000      .053475       .05251        .0577
{txt}r12 {res}       .0002        30000      .092075       .08439      .096675
{txt}r13 {res}       .0005        75000        .1616       .13472       .16705
{txt}r14 {res}        .001       150000       .21135       .17543      .226125
{txt}r15 {res}           .            .            .            .            .
{reset}
{com}. 
. xml_tab PANELA, save("tables`c(dirsep)'Table1a") replace        


{txt}note: results saved to tables/Table1a.xml

{com}.         
. 
. 
. 
{txt}end of do-file

{com}. 
. *Figure 3
. do do_files`c(dirsep)'Figure3.do
{txt}
{com}. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. 
. global LIST = "52201  69006 69106 " //M1GerusoEtAl correctedEconomist correctedSilver 
{txt}
{com}. 
. global SDCF = .067 //in points, = SD/TURNOUT*100
{txt}
{com}. global SD = 100000
{txt}
{com}. global TURNOUT = 150000000
{txt}
{com}. 
. //Parameters from empirical SDs:
. global SDSEN = 12.3 //in points,
{txt}
{com}. global SDGOV =  10.4 //in points,
{txt}
{com}. global SDOECD = 9.3 //in points,
{txt}
{com}. 
. 
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. global STEP = .002      //step along horizontal axis for density plot
{txt}
{com}. global STEP = .004      //step along horizontal axis for density plot
{txt}
{com}. global BINSTART= .42                                    //rightmost point of lowest bin
{txt}
{com}. global BINEND = 1 - $BINSTART + $STEP   //rightmost point of last bin
{txt}
{com}. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define HIST              =J(200, 200, .)         //table for figure: hist
{txt}
{com}. 
. //define bin starts as first row of F
. local row=4
{txt}
{com}. forvalues i=$BINSTART($STEP)$BINEND{c -(} //to hold histogram heights
{txt}  2{com}.         matrix HIST[`row', 1] = `i'
{txt}  3{com}.         local row = `row' + 1
{txt}  4{com}. {c )-}
{txt}
{com}. 
. 
. //make a table describing hypothetical NPVs
. foreach var in CF {c -(}
{txt}  2{com}.         matrix define NPV_`var' =J(200, 2, .) 
{txt}  3{com}. 
.         local row=4
{txt}  4{com}.         forvalues i=.49(.0002).51{c -(} 
{txt}  5{com}.                 local last = `i'-.0002
{txt}  6{com}.                 local next = `i'
{txt}  7{com}.                 matrix NPV_`var'[`row', 1] = `i'- .0002/2       //first column for horizontal axis
{txt}  8{com}.                 *local zlast = (`last'-.5)/($SD / $TURNOUT)
.                 *local znext = (`next'-.5)/($SD / $TURNOUT)
.                 local zlast = (`last'-.5)/(${c -(}SD`var'{c )-}/100)
{txt}  9{com}.                 local znext = (`next'-.5)/(${c -(}SD`var'{c )-}/100)
{txt} 10{com}. 
.                 //probability of normal N(.5, [sigma = SD/(.5*turnout)]^2) in the range [next,last]
.                 local den = normal(`znext') - normal(`zlast') 
{txt} 11{com}.                 di `zlast' "   " `znext' "   "`den'
{txt} 12{com}.                 matrix NPV_`var'[`row',2] = `den'
{txt} 13{com}.                 local row = `row' + 1
{txt} 14{com}. 
.         {c )-}
{txt} 15{com}. {c )-}
-15.223881   -14.925373   1.115e-50
-14.925373   -14.626866   9.353e-49
-14.626866   -14.328358   7.181e-47
-14.328358   -14.029851   5.046e-45
-14.029851   -13.731343   3.244e-43
-13.731343   -13.432836   1.908e-41
-13.432836   -13.134328   1.027e-39
-13.134328   -12.835821   5.060e-38
-12.835821   -12.537313   2.281e-36
-12.537313   -12.238806   9.409e-35
-12.238806   -11.940299   3.552e-33
-11.940299   -11.641791   1.227e-31
-11.641791   -11.343284   3.878e-30
-11.343284   -11.044776   1.122e-28
-11.044776   -10.746269   2.969e-27
-10.746269   -10.447761   7.193e-26
-10.447761   -10.149254   1.595e-24
-10.149254   -9.8507463   3.235e-23
-9.8507463   -9.5522388   6.006e-22
-9.5522388   -9.2537313   1.020e-20
-9.2537313   -8.9552239   1.587e-19
-8.9552239   -8.6567164   2.258e-18
-8.6567164   -8.358209   2.941e-17
-8.358209   -8.0597015   3.506e-16
-8.0597015   -7.761194   3.824e-15
-7.761194   -7.4626866   3.818e-14
-7.4626866   -7.1641791   3.489e-13
-7.1641791   -6.8656716   2.918e-12
-6.8656716   -6.5671642   2.233e-11
-6.5671642   -6.2686567   1.564e-10
-6.2686567   -5.9701493   1.003e-09
-5.9701493   -5.6716418   5.887e-09
-5.6716418   -5.3731343   3.162e-08
-5.3731343   -5.0746269   1.554e-07
-5.0746269   -4.7761194   6.994e-07
-4.7761194   -4.4776119   2.881e-06
-4.4776119   -4.1791045   .00001086
-4.1791045   -3.880597   .00003747
-3.880597   -3.5820896   .00011833
-3.5820896   -3.2835821   .00034206
-3.2835821   -2.9850746   .00090506
-2.9850746   -2.6865672   .00219198
-2.6865672   -2.3880597   .00485927
-2.3880597   -2.0895522   .00986023
-2.0895522   -1.7910448   .01831403
-1.7910448   -1.4925373   .03113612
-1.4925373   -1.1940299   .04845397
-1.1940299   -.89552239   .06902081
-.89552239   -.59701493   .08999476
-.59701493   -.29850746   .10740923
-.29850746   -1.657e-12   .11734206
-1.657e-12   .29850746   .11734206
.29850746   .59701493   .10740923
.59701493   .89552239   .08999476
.89552239   1.1940299   .06902081
1.1940299   1.4925373   .04845397
1.4925373   1.7910448   .03113612
1.7910448   2.0895522   .01831403
2.0895522   2.3880597   .00986023
2.3880597   2.6865672   .00485927
2.6865672   2.9850746   .00219198
2.9850746   3.2835821   .00090506
3.2835821   3.5820896   .00034206
3.5820896   3.880597   .00011833
3.880597   4.1791045   .00003747
4.1791045   4.4776119   .00001086
4.4776119   4.7761194   2.881e-06
4.7761194   5.0746269   6.994e-07
5.0746269   5.3731343   1.554e-07
5.3731343   5.6716418   3.162e-08
5.6716418   5.9701493   5.887e-09
5.9701493   6.2686567   1.003e-09
6.2686567   6.5671642   1.564e-10
6.5671642   6.8656716   2.233e-11
6.8656716   7.1641791   2.918e-12
7.1641791   7.4626866   3.488e-13
7.4626866   7.761194   3.819e-14
7.761194   8.0597015   3.886e-15
8.0597015   8.358209   3.331e-16
8.358209   8.6567164   0
8.6567164   8.9552239   0
8.9552239   9.2537313   0
9.2537313   9.5522388   0
9.5522388   9.8507463   0
9.8507463   10.149254   0
10.149254   10.447761   0
10.447761   10.746269   0
10.746269   11.044776   0
11.044776   11.343284   0
11.343284   11.641791   0
11.641791   11.940299   0
11.940299   12.238806   0
12.238806   12.537313   0
12.537313   12.835821   0
12.835821   13.134328   0
13.134328   13.432836   0
13.432836   13.731343   0
13.731343   14.029851   0
14.029851   14.328358   0
14.328358   14.626866   0
14.626866   14.925373   0
{txt}
{com}. 
. foreach var in SEN GOV OECD{c -(}
{txt}  2{com}.         matrix define NPV_`var' =J(250, 2, .) 
{txt}  3{com}. 
.         local row=4
{txt}  4{com}.         forvalues i=.30(.005).72{c -(} 
{txt}  5{com}.                 local last = `i'-.005
{txt}  6{com}.                 local next = `i'
{txt}  7{com}.                 matrix NPV_`var'[`row', 1] = `i'- .005/2        //first column for horizontal axis
{txt}  8{com}.                 *local zlast = (`last'-.5)/($SD / $TURNOUT)
.                 *local znext = (`next'-.5)/($SD / $TURNOUT)
.                 local zlast = (`last'-.5)/(${c -(}SD`var'{c )-}/100)
{txt}  9{com}.                 local znext = (`next'-.5)/(${c -(}SD`var'{c )-}/100)
{txt} 10{com}. 
.                 //probability of normal N(.5, [sigma = SD/(.5*turnout)]^2) in the range [next,last]
.                 local den = normal(`znext') - normal(`zlast') 
{txt} 11{com}.                 di `zlast' "   " `znext' "   "`den'
{txt} 12{com}.                 matrix NPV_`var'[`row',2] = `den'
{txt} 13{com}.                 local row = `row' + 1
{txt} 14{com}. 
.         {c )-}
{txt} 15{com}. {c )-}
-1.6666667   -1.6260163   .00418275
-1.6260163   -1.5853659   .00446852
-1.5853659   -1.5447154   .00476594
-1.5447154   -1.504065   .00507476
-1.504065   -1.4634146   .00539467
-1.4634146   -1.4227642   .00572528
-1.4227642   -1.3821138   .00606612
-1.3821138   -1.3414634   .00641664
-1.3414634   -1.300813   .00677621
-1.300813   -1.2601626   .00714412
-1.2601626   -1.2195122   .00751957
-1.2195122   -1.1788618   .00790168
-1.1788618   -1.1382114   .00828951
-1.1382114   -1.097561   .00868201
-1.097561   -1.0569106   .00907808
-1.0569106   -1.0162602   .00947655
-1.0162602   -.97560976   .00987619
-.97560976   -.93495935   .01027568
-.93495935   -.89430894   .01067368
-.89430894   -.85365854   .0110688
-.85365854   -.81300813   .01145959
-.81300813   -.77235772   .01184459
-.77235772   -.73170732   .01222232
-.73170732   -.69105691   .01259127
-.69105691   -.6504065   .01294995
-.6504065   -.6097561   .01329686
-.6097561   -.56910569   .01363051
-.56910569   -.52845528   .01394948
-.52845528   -.48780488   .01425234
-.48780488   -.44715447   .01453774
-.44715447   -.40650407   .01480437
-.40650407   -.36585366   .015051
-.36585366   -.32520325   .01527648
-.32520325   -.28455285   .01547975
-.28455285   -.24390244   .01565982
-.24390244   -.20325203   .01581583
-.20325203   -.16260163   .01594703
-.16260163   -.12195122   .01605277
-.12195122   -.08130081   .01613254
-.08130081   -.04065041   .01618594
-.04065041   9.026e-16   .0162127
9.026e-16   .04065041   .0162127
.04065041   .08130081   .01618594
.08130081   .12195122   .01613254
.12195122   .16260163   .01605277
.16260163   .20325203   .01594703
.20325203   .24390244   .01581583
.24390244   .28455285   .01565982
.28455285   .32520325   .01547975
.32520325   .36585366   .01527648
.36585366   .40650407   .015051
.40650407   .44715447   .01480437
.44715447   .48780488   .01453774
.48780488   .52845528   .01425234
.52845528   .56910569   .01394948
.56910569   .6097561   .01363051
.6097561   .6504065   .01329686
.6504065   .69105691   .01294995
.69105691   .73170732   .01259127
.73170732   .77235772   .01222232
.77235772   .81300813   .01184459
.81300813   .85365854   .01145959
.85365854   .89430894   .0110688
.89430894   .93495935   .01067368
.93495935   .97560976   .01027568
.97560976   1.0162602   .00987619
1.0162602   1.0569106   .00947655
1.0569106   1.097561   .00907808
1.097561   1.1382114   .00868201
1.1382114   1.1788618   .00828951
1.1788618   1.2195122   .00790168
1.2195122   1.2601626   .00751957
1.2601626   1.300813   .00714412
1.300813   1.3414634   .00677621
1.3414634   1.3821138   .00641664
1.3821138   1.4227642   .00606612
1.4227642   1.4634146   .00572528
1.4634146   1.504065   .00539467
1.504065   1.5447154   .00507476
1.5447154   1.5853659   .00476594
1.5853659   1.6260163   .00446852
1.6260163   1.6666667   .00418275
1.6666667   1.7073171   .00390879
1.7073171   1.7479675   .00364674
-1.9711538   -1.9230769   .00288205
-1.9230769   -1.875   .00316117
-1.875   -1.8269231   .00345931
-1.8269231   -1.7788462   .00377683
-1.7788462   -1.7307692   .00411397
-1.7307692   -1.6826923   .00447087
-1.6826923   -1.6346154   .00484751
-1.6346154   -1.5865385   .00524376
-1.5865385   -1.5384615   .0056593
-1.5384615   -1.4903846   .00609366
-1.4903846   -1.4423077   .00654623
-1.4423077   -1.3942308   .00701617
-1.3942308   -1.3461538   .00750249
-1.3461538   -1.2980769   .008004
-1.2980769   -1.25   .00851932
-1.25   -1.2019231   .00904689
-1.2019231   -1.1538462   .00958496
-1.1538462   -1.1057692   .01013158
-1.1057692   -1.0576923   .01068466
-1.0576923   -1.0096154   .01124193
-1.0096154   -.96153846   .01180095
-.96153846   -.91346154   .01235918
-.91346154   -.86538462   .01291393
-.86538462   -.81730769   .01346244
-.81730769   -.76923077   .01400186
-.76923077   -.72115385   .01452927
-.72115385   -.67307692   .01504174
-.67307692   -.625   .01553635
-.625   -.57692308   .01601018
-.57692308   -.52884615   .01646038
-.52884615   -.48076923   .01688417
-.48076923   -.43269231   .0172789
-.43269231   -.38461538   .01764204
-.38461538   -.33653846   .01797123
-.33653846   -.28846154   .01826431
-.28846154   -.24038462   .01851932
-.24038462   -.19230769   .01873454
-.19230769   -.14423077   .01890853
-.14423077   -.09615385   .01904007
-.09615385   -.04807692   .01912828
-.04807692   1.068e-15   .01917253
1.068e-15   .04807692   .01917253
.04807692   .09615385   .01912828
.09615385   .14423077   .01904007
.14423077   .19230769   .01890853
.19230769   .24038462   .01873454
.24038462   .28846154   .01851932
.28846154   .33653846   .01826431
.33653846   .38461538   .01797123
.38461538   .43269231   .01764204
.43269231   .48076923   .0172789
.48076923   .52884615   .01688417
.52884615   .57692308   .01646038
.57692308   .625   .01601018
.625   .67307692   .01553635
.67307692   .72115385   .01504174
.72115385   .76923077   .01452927
.76923077   .81730769   .01400186
.81730769   .86538462   .01346244
.86538462   .91346154   .01291393
.91346154   .96153846   .01235918
.96153846   1.0096154   .01180095
1.0096154   1.0576923   .01124193
1.0576923   1.1057692   .01068466
1.1057692   1.1538462   .01013158
1.1538462   1.2019231   .00958496
1.2019231   1.25   .00904689
1.25   1.2980769   .00851932
1.2980769   1.3461538   .008004
1.3461538   1.3942308   .00750249
1.3942308   1.4423077   .00701617
1.4423077   1.4903846   .00654623
1.4903846   1.5384615   .00609366
1.5384615   1.5865385   .0056593
1.5865385   1.6346154   .00524376
1.6346154   1.6826923   .00484751
1.6826923   1.7307692   .00447087
1.7307692   1.7788462   .00411397
1.7788462   1.8269231   .00377683
1.8269231   1.875   .00345931
1.875   1.9230769   .00316117
1.9230769   1.9711538   .00288205
1.9711538   2.0192308   .00262152
2.0192308   2.0673077   .00237903
-2.2043011   -2.1505376   .00200477
-2.1505376   -2.0967742   .00225043
-2.0967742   -2.0430108   .0025189
-2.0430108   -1.9892473   .00281127
-1.9892473   -1.9354839   .00312852
-1.9354839   -1.8817204   .00347152
-1.8817204   -1.827957   .00384101
-1.827957   -1.7741935   .00423757
-1.7741935   -1.7204301   .00466157
-1.7204301   -1.6666667   .00511321
-1.6666667   -1.6129032   .00559241
-1.6129032   -1.5591398   .00609888
-1.5591398   -1.5053763   .00663202
-1.5053763   -1.4516129   .00719096
-1.4516129   -1.3978495   .0077745
-1.3978495   -1.344086   .00838115
-1.344086   -1.2903226   .00900905
-1.2903226   -1.2365591   .00965606
-1.2365591   -1.1827957   .01031966
-1.1827957   -1.1290323   .01099705
-1.1290323   -1.0752688   .01168508
-1.0752688   -1.0215054   .01238034
-1.0215054   -.96774194   .0130791
-.96774194   -.91397849   .01377744
-.91397849   -.86021505   .01447118
-.86021505   -.80645161   .01515599
-.80645161   -.75268817   .01582741
-.75268817   -.69892473   .01648088
-.69892473   -.64516129   .0171118
-.64516129   -.59139785   .01771561
-.59139785   -.53763441   .01828779
-.53763441   -.48387097   .01882399
-.48387097   -.43010753   .01931999
-.43010753   -.37634409   .01977184
-.37634409   -.32258065   .02017587
-.32258065   -.2688172   .02052875
-.2688172   -.21505376   .02082752
-.21505376   -.16129032   .02106967
-.16129032   -.10752688   .02125313
-.10752688   -.05376344   .02137632
-.05376344   1.194e-15   .02143818
1.194e-15   .05376344   .02143818
.05376344   .10752688   .02137632
.10752688   .16129032   .02125313
.16129032   .21505376   .02106967
.21505376   .2688172   .02082752
.2688172   .32258065   .02052875
.32258065   .37634409   .02017587
.37634409   .43010753   .01977184
.43010753   .48387097   .01931999
.48387097   .53763441   .01882399
.53763441   .59139785   .01828779
.59139785   .64516129   .01771561
.64516129   .69892473   .0171118
.69892473   .75268817   .01648088
.75268817   .80645161   .01582741
.80645161   .86021505   .01515599
.86021505   .91397849   .01447118
.91397849   .96774194   .01377744
.96774194   1.0215054   .0130791
1.0215054   1.0752688   .01238034
1.0752688   1.1290323   .01168508
1.1290323   1.1827957   .01099705
1.1827957   1.2365591   .01031966
1.2365591   1.2903226   .00965606
1.2903226   1.344086   .00900905
1.344086   1.3978495   .00838115
1.3978495   1.4516129   .0077745
1.4516129   1.5053763   .00719096
1.5053763   1.5591398   .00663202
1.5591398   1.6129032   .00609888
1.6129032   1.6666667   .00559241
1.6666667   1.7204301   .00511321
1.7204301   1.7741935   .00466157
1.7741935   1.827957   .00423757
1.827957   1.8817204   .00384101
1.8817204   1.9354839   .00347152
1.9354839   1.9892473   .00312852
1.9892473   2.0430108   .00281127
2.0430108   2.0967742   .0025189
2.0967742   2.1505376   .00225043
2.1505376   2.2043011   .00200477
2.2043011   2.2580645   .00178077
2.2580645   2.311828   .00157724
{txt}
{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 15{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 16{com}.         {c )-}
{txt} 17{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 18{com}.         qui gen pvshare = rvotes/pvotes
{txt} 19{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 20{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 21{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 22{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 23{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 24{com}.         {c )-}
{txt} 25{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 26{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 27{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 28{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 29{com}.         qui gen cf_revn_total  = revn_total
{txt} 30{com}.         qui gen cf_rwin = rwin
{txt} 31{com}. 
.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 32{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 33{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 34{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 35{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim
{txt} 36{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim
{txt} 37{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 38{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 39{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 40{com}.                 label var vmargin_`i' "The margin in vote counts in state `i'"
{txt} 41{com}.                 foreach marg in $MARGLIST{c -(}
{txt} 42{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 43{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 44{com}.                 {c )-}
{txt} 45{com}.         {c )-}
{txt} 46{com}.         
.         gen closest_flippable_state=.
{txt} 47{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 48{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 49{com}.         qui gen tipping_state = .
{txt} 50{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 51{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 52{com}.         {c )-}
{txt} 53{com}. 
.         //EC Reversals
.         egen swingers = rowtotal(swing_*)
{txt} 54{com}.         foreach marg in $MARGLIST{c -(}
{txt} 55{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 56{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 57{com}.                 qui gen notReversed`marg' = swing`marg'==0
{txt} 58{com}.                 label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
{txt} 59{com}.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 60{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 61{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 62{com}.                 
.         {c )-}
{txt} 63{com}.         drop swing`marg'_*
{txt} 64{com}.         
.         //NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 65{com}.                 qui gen PVswing`marg'=0
{txt} 66{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt} 67{com}.                 
.         {c )-}       
{txt} 68{com}. 
.         
.         qui replace swingers = 0 if swingers == .
{txt} 69{com}.         qui gen ec_margin = min_vmargin
{txt} 70{com}.         qui gen pv_margin = margin_votes
{txt} 71{com}.         
.         qui gen zero = swingers == 0
{txt} 72{com}.         label var swingers "number of states that could swing election"
{txt} 73{com}.         
.         *hist swingers, ///
>         *       name(swingers, replace) 
. 
.                 
.         //more state level variables
.         forvalues s=1(1)$STATECOUNT{c -(}
{txt} 74{com}.                 //rename whig variables, as needed
.                 cap rename  W_pvShare_`s'  R_pvShare_`s'
{txt} 75{com}.                 //state vote counts for Rs
.                 qui gen r_votes_`s' = R_pvShare_`s'*twoPartyVotes_`s'
{txt} 76{com}.                 //state evs for Rs
.                 qui gen r_ev_`s' = total_ev_`s' if R_pvShare_`s'>=0.50
{txt} 77{com}.                 qui replace r_ev_`s' = 0 if R_pvShare_`s'<0.50
{txt} 78{com}.         
.         {c )-}
{txt} 79{com}.         
.         //buildup string for row totals
.         local votestring = "r_votes_1"
{txt} 80{com}.         local evstring = "r_ev_1"
{txt} 81{com}.         local totalvotesstring = "twoPartyVotes_1"
{txt} 82{com}.         local totalevstring = "total_ev_1"
{txt} 83{com}.         forvalues s=2(1)$STATECOUNT{c -(} 
{txt} 84{com}.                 local votestring = "`votestring' r_votes_`s'"   // R votes row total string
{txt} 85{com}.                 local evstring = "`evstring' r_ev_`s'"                  // R ev row total string
{txt} 86{com}.                 local totalvotesstring = "`totalvotesstring' twoPartyVotes_`s'" //all votes
{txt} 87{com}.                 local totalevstring = "`totalevstring' total_ev_`s'" //all evs
{txt} 88{com}.         {c )-}
{txt} 89{com}.         
.         //sum to national outcomes
.         qui egen r_votes_natl = rowtotal(`votestring')
{txt} 90{com}.         qui egen r_ev_natl = rowtotal(`evstring')
{txt} 91{com}.         qui egen totalvotes_natl = rowtotal(`totalvotesstring')
{txt} 92{com}.         qui egen totalev_natl = rowtotal(`totalevstring')
{txt} 93{com}.         
.         qui gen cutoff = floor(totalev_natl/2) + 1 
{txt} 94{com}.         qui gen r_share_natl = r_votes_natl/totalvotes_natl
{txt} 95{com}.         qui gen r_ev_win = (r_ev_natl >= cutoff)
{txt} 96{com}.         qui gen r_pv_win = (r_votes_natl>=(totalvotes_natl/2))
{txt} 97{com}.         qui gen inv = r_ev_win!=r_pv_win 
{txt} 98{com}.         label var r_share_natl "R share nationally"
{txt} 99{com}.         label var r_ev_win "R ev win nationally"
{txt}100{com}.         label var r_pv_win "R pv win nationally"
{txt}101{com}.         label var inv "inversion"
{txt}102{com}. 
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt}103{com}.         di `col'
{txt}104{com}.         di `file'
{txt}105{com}.         matrix REC[1, `col']            = `file'
{txt}106{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt}107{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt}108{com}.         matrix RPV[1, `col']            = `file'
{txt}109{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}110{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}111{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}112{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}113{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}114{com}.                 matrix RPV[`row',1] = `marg'
{txt}115{com}.                 
.                 qui summ Reversed`marg'
{txt}116{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}117{com}.                 qui summ Reversed`marg' if dwin==1
{txt}118{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}119{com}.                 qui summ Reversed`marg' if rwin==1
{txt}120{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}121{com}.                 
.                 qui summ PVswing`marg'
{txt}122{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}123{com}.                 
.                 local row = `row'+1
{txt}124{com}.         {c )-}
{txt}125{com}.         
.         matrix list REC
{txt}126{com}.         
.         ************************************************
.         *Data for density figures
.         ************************************************
.         local col = $LOOP
{txt}127{com}.         
.         matrix HIST[1,`col']    = `file'
{txt}128{com}. 
.         local row = 4
{txt}129{com}.         forvalues i= $BINSTART($STEP)$BINEND {c -(}
{txt}130{com}.                 local last = `i'-$STEP
{txt}131{com}.                 local next = `i'
{txt}132{com}.                 matrix HIST[`row', 1] = `i'- $STEP/2    // first column for horizontal axis
{txt}133{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' 
{txt}134{com}.                 local den = r(N)
{txt}135{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' & r_ev_win==1 
{txt}136{com}.                 local num = r(N)        
{txt}137{com}.                 local win = `num'/`den'
{txt}138{com}.                 matrix HIST[`row',`col'] = `den'
{txt}139{com}.                 local row = `row' + 1
{txt}140{com}.         {c )-}
{txt}141{com}. 
. ************************************************
. *Outer loop - close datasets/convert matrices
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}142{com}. di "$LOOP"
{txt}143{com}.         
. {c )-}
51
{txt}(100,000 missing values generated)
2
52201
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201       .       .       .       .       .       .       .       .
{txt} r2 {res}     50  .00022       .       .       .       .       .       .       .       .
{txt} r3 {res}     75  .00036       .       .       .       .       .       .       .       .
{txt} r4 {res}    100  .00047       .       .       .       .       .       .       .       .
{txt} r5 {res}    150  .00073       .       .       .       .       .       .       .       .
{txt} r6 {res}    225  .00112       .       .       .       .       .       .       .       .
{txt} r7 {res}    330  .00162       .       .       .       .       .       .       .       .
{txt} r8 {res}    500  .00237       .       .       .       .       .       .       .       .
{txt} r9 {res}    750  .00335       .       .       .       .       .       .       .       .
{txt}r10 {res}   1000  .00466       .       .       .       .       .       .       .       .
{txt}r11 {res}   1500  .00678       .       .       .       .       .       .       .       .
{txt}r12 {res}   2250     .01       .       .       .       .       .       .       .       .
{txt}r13 {res}   3300  .01436       .       .       .       .       .       .       .       .
{txt}r14 {res}   5000  .02153       .       .       .       .       .       .       .       .
{txt}r15 {res}   7500  .03137       .       .       .       .       .       .       .       .
{txt}r16 {res}  10000  .04034       .       .       .       .       .       .       .       .
{txt}r17 {res}  15000  .05617       .       .       .       .       .       .       .       .
{txt}r18 {res}  22500   .0747       .       .       .       .       .       .       .       .
{txt}r19 {res}  33000  .09467       .       .       .       .       .       .       .       .
{txt}r20 {res}  50000  .11716       .       .       .       .       .       .       .       .
{txt}r21 {res}  75000  .14006       .       .       .       .       .       .       .       .
{txt}r22 {res} 100000  .15675       .       .       .       .       .       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}3
51
(40,000 missing values generated)
3
69006
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    52201    69006        .        .        .        .        .        .
{txt} r2 {res}      50   .00022   .00175        .        .        .        .        .        .
{txt} r3 {res}      75   .00036   .00175        .        .        .        .        .        .
{txt} r4 {res}     100   .00047   .00175        .        .        .        .        .        .
{txt} r5 {res}     150   .00073  .001775        .        .        .        .        .        .
{txt} r6 {res}     225   .00112    .0019        .        .        .        .        .        .
{txt} r7 {res}     330   .00162  .002125        .        .        .        .        .        .
{txt} r8 {res}     500   .00237    .0024        .        .        .        .        .        .
{txt} r9 {res}     750   .00335  .003475        .        .        .        .        .        .
{txt}r10 {res}    1000   .00466  .004875        .        .        .        .        .        .
{txt}r11 {res}    1500   .00678    .0072        .        .        .        .        .        .
{txt}r12 {res}    2250      .01    .0098        .        .        .        .        .        .
{txt}r13 {res}    3300   .01436  .013925        .        .        .        .        .        .
{txt}r14 {res}    5000   .02153  .021525        .        .        .        .        .        .
{txt}r15 {res}    7500   .03137  .030925        .        .        .        .        .        .
{txt}r16 {res}   10000   .04034    .0407        .        .        .        .        .        .
{txt}r17 {res}   15000   .05617   .05715        .        .        .        .        .        .
{txt}r18 {res}   22500    .0747   .07965        .        .        .        .        .        .
{txt}r19 {res}   33000   .09467  .104475        .        .        .        .        .        .
{txt}r20 {res}   50000   .11716   .13705        .        .        .        .        .        .
{txt}r21 {res}   75000   .14006    .1684        .        .        .        .        .        .
{txt}r22 {res}  100000   .15675  .189875        .        .        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}4
51
(40,000 missing values generated)
4
69106
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    52201    69006    69106        .        .        .        .        .
{txt} r2 {res}      50   .00022   .00175  .000725        .        .        .        .        .
{txt} r3 {res}      75   .00036   .00175  .000725        .        .        .        .        .
{txt} r4 {res}     100   .00047   .00175  .000725        .        .        .        .        .
{txt} r5 {res}     150   .00073  .001775   .00095        .        .        .        .        .
{txt} r6 {res}     225   .00112    .0019  .000975        .        .        .        .        .
{txt} r7 {res}     330   .00162  .002125  .001675        .        .        .        .        .
{txt} r8 {res}     500   .00237    .0024    .0024        .        .        .        .        .
{txt} r9 {res}     750   .00335  .003475  .003875        .        .        .        .        .
{txt}r10 {res}    1000   .00466  .004875  .004975        .        .        .        .        .
{txt}r11 {res}    1500   .00678    .0072  .007975        .        .        .        .        .
{txt}r12 {res}    2250      .01    .0098  .011475        .        .        .        .        .
{txt}r13 {res}    3300   .01436  .013925   .01625        .        .        .        .        .
{txt}r14 {res}    5000   .02153  .021525  .024225        .        .        .        .        .
{txt}r15 {res}    7500   .03137  .030925   .03365        .        .        .        .        .
{txt}r16 {res}   10000   .04034    .0407    .0437        .        .        .        .        .
{txt}r17 {res}   15000   .05617   .05715   .06215        .        .        .        .        .
{txt}r18 {res}   22500    .0747   .07965  .084325        .        .        .        .        .
{txt}r19 {res}   33000   .09467  .104475  .110125        .        .        .        .        .
{txt}r20 {res}   50000   .11716   .13705  .142575        .        .        .        .        .
{txt}r21 {res}   75000   .14006    .1684  .174475        .        .        .        .        .
{txt}r22 {res}  100000   .15675  .189875  .199475        .        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}5

{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. cap drop REC*
{txt}
{com}. svmat REC
{txt}
{com}. cap drop RPV*
{txt}
{com}. svmat RPV
{txt}
{com}. cap drop HIST*
{txt}
{com}. svmat HIST
{txt}
{com}. 
. 
. *foreach var in HIST2 HIST3 HIST4 HIST5 HIST6 HIST7 {c -(} //NPV2{c -(}
. cap drop NPV*
{txt}
{com}. foreach var in CF SEN GOV OECD{c -(} 
{txt}  2{com}.         svmat NPV_`var'
{txt}  3{com}. {c )-}
{txt}
{com}. foreach var in NPV_CF2{c -(}
{txt}  2{com}.         qui summ HIST3 in 2/200, d
{txt}  3{com}.         local max = r(max)
{txt}  4{com}.         qui summ `var' in 2/200, d
{txt}  5{com}.         replace `var' = 5*`var'*`max'/r(max)
{txt}  6{com}.         
.         cap drop ALT`var'
{txt}  7{com}.         qui summ NPV_OECD2 in 2/200, d
{txt}  8{com}.         local max = r(max)
{txt}  9{com}.         qui summ `var' in 2/200, d
{txt} 10{com}.         gen ALT`var' = 5*`var'*`max'/r(max)
{txt} 11{com}. {c )-}
{txt}(72 real changes made)
(39,899 missing values generated)

{com}. 
. 
. //align the bar heights of histograms, given the 100k v 40k sim runs
. replace HIST2=HIST2*4/10
{txt}(43 real changes made)

{com}. 
. ************************************************
. *Plot
. ************************************************
. 
. local labsize = "medlarge"
{txt}
{com}. twoway ///
>         (connected HIST2 HIST1 in 2/100 , lw(medium) lcolor(black) ms(none) ) ///
>         (connected HIST3 HIST1 in 2/100 , lw(medium) lpattern(longdash) lcolor(black) ms(none) ) ///
>         (connected HIST4 HIST1 in 2/100 , lw(thin)  lpattern(shortdash) lcolor(black) ms(none) ) ///
>         (connected NPV_CF2 NPV_CF1 in 2/100 , lw(thin) lcolor(red) ms(none)) ///
>         , ///
>         aspect(.5) ///
>         ylabel(0 " ", nogrid labsize(medium) axis(1)) ///
>         xlabel(.42(.02).58, nogrid labsize(`labsize')) ///
>         xline(.5, lcolor(gs11) lpattern(dash) ) ///
>         xtitle("Republican Share of NPV", size(`labsize')) ///
>         ytitle("Probability", size(medium) axis(1)) ///
>         graphr(c(white) lc(white)) ///
>         legend(order(2 "Gelman et al., 2020" 1 "Geruso et al., 2022"  3 "Silver, 2020"  4 "Counterfactual NPV with" "vulnerability matched to EC") position(2) col(1) ring(0) region(lwidth(thin)) size(small))  ///
>         name(histoverlay_cfModels, replace) 
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'Figure3a.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/Figure3a.pdf written in PDF format)

{com}.         
. local labsize = "medlarge"
{txt}
{com}. twoway ///
>         (connected NPV_SEN2 NPV_SEN1 in 2/250 , lw(thick) lcolor(gs13) ms(none) ) ///
>         (connected NPV_GOV2 NPV_GOV1 in 2/250 , lw(medthick)  lpattern(solid) lcolor(gs6) ms(none) ) ///
>         (connected NPV_OECD2 NPV_OECD1 in 2/250 , lw(thin)    lpattern(solid) lcolor(black) ms(none) ) ///
>         (connected ALTNPV_CF2 NPV_CF1 in 2/200 , lw(thin) lcolor(red) ms(none)) ///
>         , ///
>         aspect(.5) ///
>         ylabel(0 " ", nogrid labsize(medium) axis(1)) ///
>         xlabel(.3(.05).7, nogrid labsize(`labsize')) ///
>         xline(.5, lcolor(gs11) lpattern(dash) ) ///
>         xtitle("Index Party Share of NPV", size(`labsize')) ///
>         ytitle("Probability", size(medium) axis(1)) ///
>         graphr(c(white) lc(white)) ///
>         legend(order(1 "Senate Average Spread" 2 "Governor Average Spread" 3 "OECD Average Spread" 4 "Counterfactual NPV with" "vulnerability matched to EC") position(2) col(1) ring(0) region(lwidth(thin)) size(small))  ///
>         name(histoverlay_cfPops, replace) 
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'Figure3b.pdf", replace  
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/Figure3b.pdf written in PDF format)

{com}. 
. 
.         
.         
.         
. 
.         
. 
{txt}end of do-file

{com}. 
. *Figure A2, Panels B and C of Table 2 
. do do_files`c(dirsep)'FigureA2Table2bc_Intermediate.do
{txt}
{com}. 
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Summarize reversal margins for pivotal single states, two together, three together
. ************************************************
. 
. 
. global LIST = "52201 69006 69106"
{txt}
{com}. 
. ************************************************
. *Loop over three datasets
. ************************************************
. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
. //make vars that summarize each election
. forvalues i = 1(1)`state_count'{c -(}
{txt} 14{com}.         qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 15{com}.         qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 16{com}.         qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 17{com}.         qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2
{txt} 18{com}.         label var vmargin_`i' "The margin in vote counts in state `i'"
{txt} 19{com}. {c )-}
{txt} 20{com}. qui egen revn_total= rowtotal(rev_*)
{txt} 21{com}. qui gen rwin = revn_total/totalEV > 0.5
{txt} 22{com}. 
. 
. *************
. * ONE STATE *
. *************
. qui gen cf_revn_total = .
{txt} 23{com}. qui gen cf_rwin = .
{txt} 24{com}. qui gen curr_min1 = .
{txt} 25{com}. qui gen flip1_state = .
{txt} 26{com}. qui gen use_i1 = .
{txt} 27{com}. qui gen use_i2 = .
{txt} 28{com}. 
. 
. //This loop identifies which states can flip election on their own (swing==1) and if so, 
. //how many votes must be dropped to do it(vmargin==X)
. forvalues i = 1(1)`state_count' {c -(}
{txt} 29{com}.         qui replace use_i1 = (rwin_`i' == rwin) & (vmargin_`i'<= curr_min1)
{txt} 30{com}.         *substract (add) state from (to) ECV count if R won (lost) the sim
.         qui replace cf_revn_total = revn_total + (-1)^rwin_`i' * total_ev_`i'
{txt} 31{com}.         qui replace cf_rwin =  cf_revn_total/totalEV > 0.5 //did R win in the counterfactual flip of the state?
{txt} 32{com}.         qui replace use_i2 = (cf_rwin != rwin) & use_i1
{txt} 33{com}.         qui replace curr_min = vmargin_`i' if use_i2
{txt} 34{com}.         qui replace flip1_state = `i' if use_i2
{txt} 35{com}. {c )-}
{txt} 36{com}. 
. qui gen min_vmargin1 = curr_min1
{txt} 37{com}. label var min_vmargin1 "minimum votes needed for 1-state flip"
{txt} 38{com}. 
. drop use_* curr_min1
{txt} 39{com}. 
. */
. 
. 
. *************
. * TWO STATE *
. *************
. 
. * initialize
. qui gen cf_revn_total2 = .
{txt} 40{com}. qui gen curr_min2 = .
{txt} 41{com}. qui gen flip2_state1 = .
{txt} 42{com}. qui gen flip2_state2 = .
{txt} 43{com}. qui gen use_i = .
{txt} 44{com}. qui gen use_j1 = .
{txt} 45{com}. qui gen use_j2 = .
{txt} 46{com}. 
. * outer loop
. local state_count_m = `state_count' - 1
{txt} 47{com}. forvalues i = 1(1)`state_count_m' {c -(}
{txt} 48{com}.     qui replace use_i = (rwin_`i' == rwin) & (vmargin_`i'<= curr_min)
{txt} 49{com}.         *substract (add) state from (to) ECV count if R won (lost) the sim
.         qui replace cf_revn_total = revn_total + (-1)^rwin_`i' * total_ev_`i' if use_i
{txt} 50{com}.         * inner loop
.         local i_p = `i' + 1 
{txt} 51{com}.         forvalues j = `i_p'(1)`state_count' {c -(}
{txt} 52{com}.                 qui replace use_j1 = (rwin_`j'== rwin_`i') & (vmargin_`j' + vmargin_`i' <= curr_min)
{txt} 53{com}.                 * substract (add) state from (to) ECV count if R won (lost) the sim
.                 qui replace cf_revn_total2 = cf_revn_total + (-1)^rwin_`j' * total_ev_`j' if use_j1
{txt} 54{com}.                 qui replace cf_rwin = cf_revn_total2/totalEV > 0.5 if use_j1 //did R win in the counterfactual flip of the states?
{txt} 55{com}.                 
.                 qui replace use_j2 = (cf_rwin != rwin) & use_j1
{txt} 56{com}.                 qui replace curr_min2 = vmargin_`j' + vmargin_`i' if use_j2
{txt} 57{com}.                 qui replace flip2_state1 = `i' if use_j2
{txt} 58{com}.                 qui replace flip2_state2 = `j' if use_j2
{txt} 59{com}.         {c )-}
{txt} 60{com}. {c )-}
{txt} 61{com}. 
. qui gen min_vmargin2 = curr_min2
{txt} 62{com}. label var min_vmargin2 "minimum votes needed for 2-state flip"
{txt} 63{com}. 
. drop use_* curr_min2
{txt} 64{com}. 
. timer off 2
{txt} 65{com}. timer list 2
{txt} 66{com}. 
. 
. ***************
. * THREE STATE *
. ***************
. 
. * initialize
. qui gen cf_revn_total3 = .
{txt} 67{com}. qui gen curr_min3 = .
{txt} 68{com}. qui gen flip3_state1 = .
{txt} 69{com}. qui gen flip3_state2 = .
{txt} 70{com}. qui gen flip3_state3 = .
{txt} 71{com}. qui gen use_i = .
{txt} 72{com}. qui gen use_j = .
{txt} 73{com}. qui gen use_k1 = .
{txt} 74{com}. qui gen use_k2 = .
{txt} 75{com}. 
. 
. local loop = 1
{txt} 76{com}. * outer loop
. local state_count = 51
{txt} 77{com}. local state_count_m2 = `state_count' - 2
{txt} 78{com}. forvalues i = 1(1)`state_count_m2' {c -(}
{txt} 79{com}.     qui replace use_i = (rwin_`i' == rwin) & (vmargin_`i'<= curr_min3)
{txt} 80{com}.         *substract (add) state from (to) ECV count if R won (lost) the sim
.         qui replace cf_revn_total = revn_total + (-1)^rwin_`i' * total_ev_`i' if use_i
{txt} 81{com}.         * middle loop
.         local i_p = `i' + 1
{txt} 82{com}.         local state_count_m1 = `state_count' - 1
{txt} 83{com}.         forvalues j = `i_p'(1)`state_count_m1' {c -(}
{txt} 84{com}.                 qui replace use_j = (rwin_`j'== rwin_`i') & (vmargin_`j' + vmargin_`i' <= curr_min3)
{txt} 85{com}.                 *substract (add) state from (to) ECV count if R won (lost) the sim
.                 qui replace cf_revn_total2 = cf_revn_total + (-1)^rwin_`j' * total_ev_`j' if use_j
{txt} 86{com}.                 * inner loop
.                 local j_p = `j' + 1 
{txt} 87{com}.                 forvalues k = `j_p'(1)`state_count' {c -(}
{txt} 88{com}.                     qui replace use_k1 = (rwin_`k'== rwin_`j') & ///
>                                                                  (vmargin_`k' + vmargin_`j' + vmargin_`i' <= curr_min3)
{txt} 89{com}.                         qui replace cf_revn_total3 = cf_revn_total2 + (-1)^rwin_`k' * total_ev_`k' if use_k1
{txt} 90{com}.                         qui replace cf_rwin = cf_revn_total3/totalEV > 0.5 if use_k1
{txt} 91{com}.                         qui replace use_k2 = (cf_rwin != rwin) & use_k1
{txt} 92{com}.                         qui replace curr_min3 = vmargin_`k' + vmargin_`j' + vmargin_`i' if use_k2
{txt} 93{com}.                         qui replace flip3_state1 = `i' if use_k2
{txt} 94{com}.                         qui replace flip3_state2 = `j' if use_k2
{txt} 95{com}.                         qui replace flip3_state3 = `k' if use_k2
{txt} 96{com}.                         if mod(`loop',100) == 0 {c -(}
{txt} 97{com}.                             di "`loop'/19600 or so"
{txt} 98{com}.                         {c )-}
{txt} 99{com}.                         local loop = `loop'+1
{txt}100{com}.                 {c )-}
{txt}101{com}.         {c )-}
{txt}102{com}. {c )-}
{txt}103{com}. 
. qui gen min_vmargin3 = curr_min3
{txt}104{com}. label var min_vmargin3 "minimum votes needed for 3-state flip"
{txt}105{com}. 
. drop use_* curr_min3
{txt}106{com}. 
. * save
.         save "working_data`c(dirsep)'min_vote_reversal_upto3_`file'.dta", replace
{txt}107{com}. 
. * evaluate to see if the results make sense
. sum min_vmargin* if !missing(min_vmargin1), d
{txt}108{com}. sum min_vmargin2 min_vmargin3 if !missing(min_vmargin2), d
{txt}109{com}. 
. {c )-}
51
100/19600 or so
200/19600 or so
300/19600 or so
400/19600 or so
500/19600 or so
600/19600 or so
700/19600 or so
800/19600 or so
900/19600 or so
1000/19600 or so
1100/19600 or so
1200/19600 or so
1300/19600 or so
1400/19600 or so
1500/19600 or so
1600/19600 or so
1700/19600 or so
1800/19600 or so
1900/19600 or so
2000/19600 or so
2100/19600 or so
2200/19600 or so
2300/19600 or so
2400/19600 or so
2500/19600 or so
2600/19600 or so
2700/19600 or so
2800/19600 or so
2900/19600 or so
3000/19600 or so
3100/19600 or so
3200/19600 or so
3300/19600 or so
3400/19600 or so
3500/19600 or so
3600/19600 or so
3700/19600 or so
3800/19600 or so
3900/19600 or so
4000/19600 or so
4100/19600 or so
4200/19600 or so
4300/19600 or so
4400/19600 or so
4500/19600 or so
4600/19600 or so
4700/19600 or so
4800/19600 or so
4900/19600 or so
5000/19600 or so
5100/19600 or so
5200/19600 or so
5300/19600 or so
5400/19600 or so
5500/19600 or so
5600/19600 or so
5700/19600 or so
5800/19600 or so
5900/19600 or so
6000/19600 or so
6100/19600 or so
6200/19600 or so
6300/19600 or so
6400/19600 or so
6500/19600 or so
6600/19600 or so
6700/19600 or so
6800/19600 or so
6900/19600 or so
7000/19600 or so
7100/19600 or so
7200/19600 or so
7300/19600 or so
7400/19600 or so
7500/19600 or so
7600/19600 or so
7700/19600 or so
7800/19600 or so
7900/19600 or so
8000/19600 or so
8100/19600 or so
8200/19600 or so
8300/19600 or so
8400/19600 or so
8500/19600 or so
8600/19600 or so
8700/19600 or so
8800/19600 or so
8900/19600 or so
9000/19600 or so
9100/19600 or so
9200/19600 or so
9300/19600 or so
9400/19600 or so
9500/19600 or so
9600/19600 or so
9700/19600 or so
9800/19600 or so
9900/19600 or so
10000/19600 or so
10100/19600 or so
10200/19600 or so
10300/19600 or so
10400/19600 or so
10500/19600 or so
10600/19600 or so
10700/19600 or so
10800/19600 or so
10900/19600 or so
11000/19600 or so
11100/19600 or so
11200/19600 or so
11300/19600 or so
11400/19600 or so
11500/19600 or so
11600/19600 or so
11700/19600 or so
11800/19600 or so
11900/19600 or so
12000/19600 or so
12100/19600 or so
12200/19600 or so
12300/19600 or so
12400/19600 or so
12500/19600 or so
12600/19600 or so
12700/19600 or so
12800/19600 or so
12900/19600 or so
13000/19600 or so
13100/19600 or so
13200/19600 or so
13300/19600 or so
13400/19600 or so
13500/19600 or so
13600/19600 or so
13700/19600 or so
13800/19600 or so
13900/19600 or so
14000/19600 or so
14100/19600 or so
14200/19600 or so
14300/19600 or so
14400/19600 or so
14500/19600 or so
14600/19600 or so
14700/19600 or so
14800/19600 or so
14900/19600 or so
15000/19600 or so
15100/19600 or so
15200/19600 or so
15300/19600 or so
15400/19600 or so
15500/19600 or so
15600/19600 or so
15700/19600 or so
15800/19600 or so
15900/19600 or so
16000/19600 or so
16100/19600 or so
16200/19600 or so
16300/19600 or so
16400/19600 or so
16500/19600 or so
16600/19600 or so
16700/19600 or so
16800/19600 or so
16900/19600 or so
17000/19600 or so
17100/19600 or so
17200/19600 or so
17300/19600 or so
17400/19600 or so
17500/19600 or so
17600/19600 or so
17700/19600 or so
17800/19600 or so
17900/19600 or so
18000/19600 or so
18100/19600 or so
18200/19600 or so
18300/19600 or so
18400/19600 or so
18500/19600 or so
18600/19600 or so
18700/19600 or so
18800/19600 or so
18900/19600 or so
19000/19600 or so
19100/19600 or so
19200/19600 or so
19300/19600 or so
19400/19600 or so
19500/19600 or so
19600/19600 or so
19700/19600 or so
19800/19600 or so
19900/19600 or so
20000/19600 or so
20100/19600 or so
20200/19600 or so
20300/19600 or so
20400/19600 or so
20500/19600 or so
20600/19600 or so
20700/19600 or so
20800/19600 or so
{txt}(note: file working_data/min_vote_reversal_upto3_52201.dta not found)
file working_data/min_vote_reversal_upto3_52201.dta saved

            minimum votes needed for 1-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 1101.015       .8382519
{txt} 5%    {res} 6011.702       4.178248
{txt}10%    {res} 13276.05       4.971469       {txt}Obs         {res}     51,181
{txt}25%    {res} 60685.81       6.230486       {txt}Sum of Wgt. {res}     51,181

{txt}50%    {res} 508391.2                      {txt}Mean          {res}  1086140
                        {txt}Largest       Std. Dev.     {res}  1247288
{txt}75%    {res}  1871152        6233737
{txt}90%    {res}  3030536        6286665       {txt}Variance      {res} 1.56e+12
{txt}95%    {res}  3648290        6292612       {txt}Skewness      {res} 1.136941
{txt}99%    {res}  4632785        6575530       {txt}Kurtosis      {res} 3.401613

            {txt}minimum votes needed for 2-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 5899.615       330.4839
{txt} 5%    {res} 14019.96       429.4559
{txt}10%    {res} 21554.35       494.8348       {txt}Obs         {res}     51,181
{txt}25%    {res} 43951.73       559.2467       {txt}Sum of Wgt. {res}     51,181

{txt}50%    {res} 108703.1                      {txt}Mean          {res} 372893.1
                        {txt}Largest       Std. Dev.     {res} 659505.7
{txt}75%    {res} 350959.9        5782990
{txt}90%    {res}  1061489        5831945       {txt}Variance      {res} 4.35e+11
{txt}95%    {res}  1905842        5832745       {txt}Skewness      {res} 3.122094
{txt}99%    {res}  3179023        5945446       {txt}Kurtosis      {res} 14.13592

            {txt}minimum votes needed for 3-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res}  13943.1       2055.875
{txt} 5%    {res} 26773.76        2575.47
{txt}10%    {res} 36741.88       2632.042       {txt}Obs         {res}     51,181
{txt}25%    {res} 60272.95       2839.798       {txt}Sum of Wgt. {res}     51,181

{txt}50%    {res}   105235                      {txt}Mean          {res} 189597.2
                        {txt}Largest       Std. Dev.     {res}   247744
{txt}75%    {res} 213365.4        2751192
{txt}90%    {res} 426343.9        2912912       {txt}Variance      {res} 6.14e+10
{txt}95%    {res} 633269.9        3058915       {txt}Skewness      {res} 3.853598
{txt}99%    {res}  1289314        3152424       {txt}Kurtosis      {res} 24.10677

            {txt}minimum votes needed for 2-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 7084.573       330.4839
{txt} 5%    {res} 17599.54       429.4559
{txt}10%    {res} 28259.16       494.8348       {txt}Obs         {res}     74,872
{txt}25%    {res} 65481.17       559.2467       {txt}Sum of Wgt. {res}     74,872

{txt}50%    {res} 296398.3                      {txt}Mean          {res}  1028949
                        {txt}Largest       Std. Dev.     {res}  1356787
{txt}75%    {res}  1701779        8445783
{txt}90%    {res}  3160838        8820977       {txt}Variance      {res} 1.84e+12
{txt}95%    {res}  3941185        8976064       {txt}Skewness      {res} 1.525478
{txt}99%    {res}  5274771        9359055       {txt}Kurtosis      {res} 4.732643

            {txt}minimum votes needed for 3-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 16106.16       2055.875
{txt} 5%    {res} 31679.27        2575.47
{txt}10%    {res} 44083.37       2632.042       {txt}Obs         {res}     74,872
{txt}25%    {res}  77423.6       2822.356       {txt}Sum of Wgt. {res}     74,872

{txt}50%    {res} 175255.6                      {txt}Mean          {res} 534852.2
                        {txt}Largest       Std. Dev.     {res} 834823.5
{txt}75%    {res} 558701.4        6705688
{txt}90%    {res}  1616147        6795699       {txt}Variance      {res} 6.97e+11
{txt}95%    {res}  2459282        6931381       {txt}Skewness      {res} 2.650507
{txt}99%    {res}  3913526        7390960       {txt}Kurtosis      {res} 10.72691
51
100/19600 or so
200/19600 or so
300/19600 or so
400/19600 or so
500/19600 or so
600/19600 or so
700/19600 or so
800/19600 or so
900/19600 or so
1000/19600 or so
1100/19600 or so
1200/19600 or so
1300/19600 or so
1400/19600 or so
1500/19600 or so
1600/19600 or so
1700/19600 or so
1800/19600 or so
1900/19600 or so
2000/19600 or so
2100/19600 or so
2200/19600 or so
2300/19600 or so
2400/19600 or so
2500/19600 or so
2600/19600 or so
2700/19600 or so
2800/19600 or so
2900/19600 or so
3000/19600 or so
3100/19600 or so
3200/19600 or so
3300/19600 or so
3400/19600 or so
3500/19600 or so
3600/19600 or so
3700/19600 or so
3800/19600 or so
3900/19600 or so
4000/19600 or so
4100/19600 or so
4200/19600 or so
4300/19600 or so
4400/19600 or so
4500/19600 or so
4600/19600 or so
4700/19600 or so
4800/19600 or so
4900/19600 or so
5000/19600 or so
5100/19600 or so
5200/19600 or so
5300/19600 or so
5400/19600 or so
5500/19600 or so
5600/19600 or so
5700/19600 or so
5800/19600 or so
5900/19600 or so
6000/19600 or so
6100/19600 or so
6200/19600 or so
6300/19600 or so
6400/19600 or so
6500/19600 or so
6600/19600 or so
6700/19600 or so
6800/19600 or so
6900/19600 or so
7000/19600 or so
7100/19600 or so
7200/19600 or so
7300/19600 or so
7400/19600 or so
7500/19600 or so
7600/19600 or so
7700/19600 or so
7800/19600 or so
7900/19600 or so
8000/19600 or so
8100/19600 or so
8200/19600 or so
8300/19600 or so
8400/19600 or so
8500/19600 or so
8600/19600 or so
8700/19600 or so
8800/19600 or so
8900/19600 or so
9000/19600 or so
9100/19600 or so
9200/19600 or so
9300/19600 or so
9400/19600 or so
9500/19600 or so
9600/19600 or so
9700/19600 or so
9800/19600 or so
9900/19600 or so
10000/19600 or so
10100/19600 or so
10200/19600 or so
10300/19600 or so
10400/19600 or so
10500/19600 or so
10600/19600 or so
10700/19600 or so
10800/19600 or so
10900/19600 or so
11000/19600 or so
11100/19600 or so
11200/19600 or so
11300/19600 or so
11400/19600 or so
11500/19600 or so
11600/19600 or so
11700/19600 or so
11800/19600 or so
11900/19600 or so
12000/19600 or so
12100/19600 or so
12200/19600 or so
12300/19600 or so
12400/19600 or so
12500/19600 or so
12600/19600 or so
12700/19600 or so
12800/19600 or so
12900/19600 or so
13000/19600 or so
13100/19600 or so
13200/19600 or so
13300/19600 or so
13400/19600 or so
13500/19600 or so
13600/19600 or so
13700/19600 or so
13800/19600 or so
13900/19600 or so
14000/19600 or so
14100/19600 or so
14200/19600 or so
14300/19600 or so
14400/19600 or so
14500/19600 or so
14600/19600 or so
14700/19600 or so
14800/19600 or so
14900/19600 or so
15000/19600 or so
15100/19600 or so
15200/19600 or so
15300/19600 or so
15400/19600 or so
15500/19600 or so
15600/19600 or so
15700/19600 or so
15800/19600 or so
15900/19600 or so
16000/19600 or so
16100/19600 or so
16200/19600 or so
16300/19600 or so
16400/19600 or so
16500/19600 or so
16600/19600 or so
16700/19600 or so
16800/19600 or so
16900/19600 or so
17000/19600 or so
17100/19600 or so
17200/19600 or so
17300/19600 or so
17400/19600 or so
17500/19600 or so
17600/19600 or so
17700/19600 or so
17800/19600 or so
17900/19600 or so
18000/19600 or so
18100/19600 or so
18200/19600 or so
18300/19600 or so
18400/19600 or so
18500/19600 or so
18600/19600 or so
18700/19600 or so
18800/19600 or so
18900/19600 or so
19000/19600 or so
19100/19600 or so
19200/19600 or so
19300/19600 or so
19400/19600 or so
19500/19600 or so
19600/19600 or so
19700/19600 or so
19800/19600 or so
19900/19600 or so
20000/19600 or so
20100/19600 or so
20200/19600 or so
20300/19600 or so
20400/19600 or so
20500/19600 or so
20600/19600 or so
20700/19600 or so
20800/19600 or so
{txt}(note: file working_data/min_vote_reversal_upto3_69006.dta not found)
file working_data/min_vote_reversal_upto3_69006.dta saved

            minimum votes needed for 1-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 1232.815              0
{txt} 5%    {res}   7400.2              0
{txt}10%    {res} 16246.05              0       {txt}Obs         {res}     24,470
{txt}25%    {res} 61666.86              0       {txt}Sum of Wgt. {res}     24,470

{txt}50%    {res}  1080978                      {txt}Mean          {res}  1945780
                        {txt}Largest       Std. Dev.     {res}  1998561
{txt}75%    {res}  4177395        5996997
{txt}90%    {res}  4738368        6022626       {txt}Variance      {res} 3.99e+12
{txt}95%    {res}  4988956        6025472       {txt}Skewness      {res} .4099174
{txt}99%    {res}  5404701        6142224       {txt}Kurtosis      {res} 1.438027

            {txt}minimum votes needed for 2-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 8186.574       522.7708
{txt} 5%    {res} 20608.52       744.2864
{txt}10%    {res} 30944.14       893.1703       {txt}Obs         {res}     24,470
{txt}25%    {res} 58248.33       977.7456       {txt}Sum of Wgt. {res}     24,470

{txt}50%    {res} 127391.3                      {txt}Mean          {res} 614750.6
                        {txt}Largest       Std. Dev.     {res} 962877.8
{txt}75%    {res} 510318.6        5615209
{txt}90%    {res}  2289450        5728423       {txt}Variance      {res} 9.27e+11
{txt}95%    {res}  2619414        5769342       {txt}Skewness      {res} 1.877174
{txt}99%    {res}  4094608        5774843       {txt}Kurtosis      {res} 5.972855

            {txt}minimum votes needed for 3-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 20749.91       3061.351
{txt} 5%    {res} 37422.91        3393.41
{txt}10%    {res} 49851.05       3450.153       {txt}Obs         {res}     24,470
{txt}25%    {res} 77984.29       3795.073       {txt}Sum of Wgt. {res}     24,470

{txt}50%    {res} 124946.7                      {txt}Mean          {res} 197664.4
                        {txt}Largest       Std. Dev.     {res} 189459.1
{txt}75%    {res} 253390.1        1815001
{txt}90%    {res} 459996.1        1825959       {txt}Variance      {res} 3.59e+10
{txt}95%    {res} 581952.4        1837542       {txt}Skewness      {res} 2.424387
{txt}99%    {res} 817500.9        1894321       {txt}Kurtosis      {res} 12.17295

            {txt}minimum votes needed for 2-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 9897.631       522.7708
{txt} 5%    {res} 25155.42       744.2864
{txt}10%    {res} 38230.41       893.1703       {txt}Obs         {res}     34,207
{txt}25%    {res} 78832.35       977.7456       {txt}Sum of Wgt. {res}     34,207

{txt}50%    {res} 322246.5                      {txt}Mean          {res}  1417914
                        {txt}Largest       Std. Dev.     {res}  1805052
{txt}75%    {res}  2363271        7659445
{txt}90%    {res}  4681905        7674986       {txt}Variance      {res} 3.26e+12
{txt}95%    {res}  5126586        7697228       {txt}Skewness      {res} 1.138007
{txt}99%    {res}  5800530        7901621       {txt}Kurtosis      {res} 2.892016

            {txt}minimum votes needed for 3-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 22938.17       3061.351
{txt} 5%    {res}  42599.3        3393.41
{txt}10%    {res} 57461.32       3450.153       {txt}Obs         {res}     34,207
{txt}25%    {res} 94445.26       3795.073       {txt}Sum of Wgt. {res}     34,207

{txt}50%    {res} 191622.4                      {txt}Mean          {res} 645135.4
                        {txt}Largest       Std. Dev.     {res}  1068617
{txt}75%    {res} 560015.9        6228711
{txt}90%    {res}  2151363        6242794       {txt}Variance      {res} 1.14e+12
{txt}95%    {res}  2916228        6446659       {txt}Skewness      {res} 2.642409
{txt}99%    {res}  5074501        6565189       {txt}Kurtosis      {res}  9.88364
51
100/19600 or so
200/19600 or so
300/19600 or so
400/19600 or so
500/19600 or so
600/19600 or so
700/19600 or so
800/19600 or so
900/19600 or so
1000/19600 or so
1100/19600 or so
1200/19600 or so
1300/19600 or so
1400/19600 or so
1500/19600 or so
1600/19600 or so
1700/19600 or so
1800/19600 or so
1900/19600 or so
2000/19600 or so
2100/19600 or so
2200/19600 or so
2300/19600 or so
2400/19600 or so
2500/19600 or so
2600/19600 or so
2700/19600 or so
2800/19600 or so
2900/19600 or so
3000/19600 or so
3100/19600 or so
3200/19600 or so
3300/19600 or so
3400/19600 or so
3500/19600 or so
3600/19600 or so
3700/19600 or so
3800/19600 or so
3900/19600 or so
4000/19600 or so
4100/19600 or so
4200/19600 or so
4300/19600 or so
4400/19600 or so
4500/19600 or so
4600/19600 or so
4700/19600 or so
4800/19600 or so
4900/19600 or so
5000/19600 or so
5100/19600 or so
5200/19600 or so
5300/19600 or so
5400/19600 or so
5500/19600 or so
5600/19600 or so
5700/19600 or so
5800/19600 or so
5900/19600 or so
6000/19600 or so
6100/19600 or so
6200/19600 or so
6300/19600 or so
6400/19600 or so
6500/19600 or so
6600/19600 or so
6700/19600 or so
6800/19600 or so
6900/19600 or so
7000/19600 or so
7100/19600 or so
7200/19600 or so
7300/19600 or so
7400/19600 or so
7500/19600 or so
7600/19600 or so
7700/19600 or so
7800/19600 or so
7900/19600 or so
8000/19600 or so
8100/19600 or so
8200/19600 or so
8300/19600 or so
8400/19600 or so
8500/19600 or so
8600/19600 or so
8700/19600 or so
8800/19600 or so
8900/19600 or so
9000/19600 or so
9100/19600 or so
9200/19600 or so
9300/19600 or so
9400/19600 or so
9500/19600 or so
9600/19600 or so
9700/19600 or so
9800/19600 or so
9900/19600 or so
10000/19600 or so
10100/19600 or so
10200/19600 or so
10300/19600 or so
10400/19600 or so
10500/19600 or so
10600/19600 or so
10700/19600 or so
10800/19600 or so
10900/19600 or so
11000/19600 or so
11100/19600 or so
11200/19600 or so
11300/19600 or so
11400/19600 or so
11500/19600 or so
11600/19600 or so
11700/19600 or so
11800/19600 or so
11900/19600 or so
12000/19600 or so
12100/19600 or so
12200/19600 or so
12300/19600 or so
12400/19600 or so
12500/19600 or so
12600/19600 or so
12700/19600 or so
12800/19600 or so
12900/19600 or so
13000/19600 or so
13100/19600 or so
13200/19600 or so
13300/19600 or so
13400/19600 or so
13500/19600 or so
13600/19600 or so
13700/19600 or so
13800/19600 or so
13900/19600 or so
14000/19600 or so
14100/19600 or so
14200/19600 or so
14300/19600 or so
14400/19600 or so
14500/19600 or so
14600/19600 or so
14700/19600 or so
14800/19600 or so
14900/19600 or so
15000/19600 or so
15100/19600 or so
15200/19600 or so
15300/19600 or so
15400/19600 or so
15500/19600 or so
15600/19600 or so
15700/19600 or so
15800/19600 or so
15900/19600 or so
16000/19600 or so
16100/19600 or so
16200/19600 or so
16300/19600 or so
16400/19600 or so
16500/19600 or so
16600/19600 or so
16700/19600 or so
16800/19600 or so
16900/19600 or so
17000/19600 or so
17100/19600 or so
17200/19600 or so
17300/19600 or so
17400/19600 or so
17500/19600 or so
17600/19600 or so
17700/19600 or so
17800/19600 or so
17900/19600 or so
18000/19600 or so
18100/19600 or so
18200/19600 or so
18300/19600 or so
18400/19600 or so
18500/19600 or so
18600/19600 or so
18700/19600 or so
18800/19600 or so
18900/19600 or so
19000/19600 or so
19100/19600 or so
19200/19600 or so
19300/19600 or so
19400/19600 or so
19500/19600 or so
19600/19600 or so
19700/19600 or so
19800/19600 or so
19900/19600 or so
20000/19600 or so
20100/19600 or so
20200/19600 or so
20300/19600 or so
20400/19600 or so
20500/19600 or so
20600/19600 or so
20700/19600 or so
20800/19600 or so
{txt}(note: file working_data/min_vote_reversal_upto3_69106.dta not found)
file working_data/min_vote_reversal_upto3_69106.dta saved

            minimum votes needed for 1-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 1190.303              0
{txt} 5%    {res} 6166.649              0
{txt}10%    {res} 13546.24              0       {txt}Obs         {res}     22,684
{txt}25%    {res} 49413.99              0       {txt}Sum of Wgt. {res}     22,684

{txt}50%    {res} 313590.6                      {txt}Mean          {res}  1288194
                        {txt}Largest       Std. Dev.     {res}  1629086
{txt}75%    {res}  2187571        8445913
{txt}90%    {res}  3990879        8723552       {txt}Variance      {res} 2.65e+12
{txt}95%    {res}  4617346        8773385       {txt}Skewness      {res} 1.198218
{txt}99%    {res}  5742139        9062414       {txt}Kurtosis      {res} 3.383228

            {txt}minimum votes needed for 2-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 6782.493       297.7234
{txt} 5%    {res} 17452.12       396.0236
{txt}10%    {res} 26461.21       455.1074       {txt}Obs         {res}     22,684
{txt}25%    {res} 53082.85       591.8446       {txt}Sum of Wgt. {res}     22,684

{txt}50%    {res} 110475.5                      {txt}Mean          {res} 419218.3
                        {txt}Largest       Std. Dev.     {res}   785534
{txt}75%    {res} 284608.6        6260845
{txt}90%    {res}  1731988        6617270       {txt}Variance      {res} 6.17e+11
{txt}95%    {res}  2334683        6783080       {txt}Skewness      {res} 2.882398
{txt}99%    {res}  3568483        8797058       {txt}Kurtosis      {res} 12.02955

            {txt}minimum votes needed for 3-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res}  16698.4       1697.887
{txt} 5%    {res} 31871.75       2227.226
{txt}10%    {res} 44276.04       2875.444       {txt}Obs         {res}     22,684
{txt}25%    {res} 72247.73       3486.753       {txt}Sum of Wgt. {res}     22,684

{txt}50%    {res} 116967.1                      {txt}Mean          {res} 165009.5
                        {txt}Largest       Std. Dev.     {res} 166787.7
{txt}75%    {res} 193565.2        1956399
{txt}90%    {res} 334605.4        1963704       {txt}Variance      {res} 2.78e+10
{txt}95%    {res}   455693        2034010       {txt}Skewness      {res} 3.651727
{txt}99%    {res} 898763.9        2037634       {txt}Kurtosis      {res} 23.42017

            {txt}minimum votes needed for 2-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 8115.577       297.7234
{txt} 5%    {res}  20966.8       396.0236
{txt}10%    {res} 33499.38       455.1074       {txt}Obs         {res}     31,689
{txt}25%    {res} 71677.57       591.8446       {txt}Sum of Wgt. {res}     31,689

{txt}50%    {res} 219842.6                      {txt}Mean          {res}  1040369
                        {txt}Largest       Std. Dev.     {res}  1479045
{txt}75%    {res}  1622628        7869821
{txt}90%    {res}  3575211        8262861       {txt}Variance      {res} 2.19e+12
{txt}95%    {res}  4437908        8692096       {txt}Skewness      {res} 1.624978
{txt}99%    {res}  5615001        8797058       {txt}Kurtosis      {res} 4.761018

            {txt}minimum votes needed for 3-state flip
{hline 61}
      Percentiles      Smallest
 1%    {res} 18885.91       1697.887
{txt} 5%    {res}  37232.6       2227.226
{txt}10%    {res}  52126.9       2875.444       {txt}Obs         {res}     31,689
{txt}25%    {res} 87520.27       3486.753       {txt}Sum of Wgt. {res}     31,689

{txt}50%    {res} 163822.6                      {txt}Mean          {res}   516008
                        {txt}Largest       Std. Dev.     {res} 873425.2
{txt}75%    {res} 448354.9        6663049
{txt}90%    {res}  1458951        6700882       {txt}Variance      {res} 7.63e+11
{txt}95%    {res}  2543610        6746144       {txt}Skewness      {res} 2.995019
{txt}99%    {res}  4405628        6847119       {txt}Kurtosis      {res} 12.76444
{txt}
{com}. 
. 
{txt}end of do-file

{com}. do do_files`c(dirsep)'FigureA2Table2bc.do
{txt}
{com}. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .00015 .0002 .0003 .00045 .0007 .001"
{txt}
{com}. global MARGLISTPCT2 = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"
{txt}
{com}. 
. global LIST = "52201 69006 69106 " // M1GerusoEtAl correctedEconomist correctedSilver
{txt}
{com}.  
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECPCT    =J(30, 10, .)           //table of summary of reversal rates based on % margins;        columns are models, rows are %critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define REC1STATE         =J(30, 10, .)           //1 state, replicates REC table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define REC2STATE         =J(30, 10, .)           //2 states combined; table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define REC3STATE         =J(30, 10, .)           //3 states combined; table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECPCT1STATE              =J(30, 10, .)           //1 state, replicates REC table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECPCT2STATE              =J(30, 10, .)           //2 states combined; table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECPCT3STATE              =J(30, 10, .)           //3 states combined; table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define RPVPCT    =J(30, 10, .)           //table of summary of NPV reversal rates; in % margins (pp)
{txt}
{com}. matrix define RECPCT1STATE2             =J(30, 10, .)           // like RECPCTXSTATE, but uses MARGLISTPCT2 
{txt}
{com}. matrix define RECPCT2STATE2             =J(30, 10, .)           // like RECPCTXSTATE, but uses MARGLISTPCT2 
{txt}
{com}. matrix define RECPCT3STATE2             =J(30, 10, .)           // like RECPCTXSTATE, but uses MARGLISTPCT2 
{txt}
{com}. 
. 
. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "working_data`c(dirsep)'min_vote_reversal_upto3_`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 15{com}.         qui gen pvshare = rvotes/pvotes
{txt} 16{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 17{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 18{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 19{com}.         
.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 20{com}.         
.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 21{com}.                 ren vmargin_`i' old_vmargin_`i'
{txt} 22{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 23{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 24{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 25{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim
{txt} 26{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim
{txt} 27{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 28{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 29{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 30{com}.                 label var vmargin_`i' "The margin in vote counts in state `i'"
{txt} 31{com}.                 
.                 //Levels: cylce through marglist to get probability of flips at each margin
.                 foreach marg in $MARGLIST{c -(}
{txt} 32{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 33{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 34{com}.                 {c )-}
{txt} 35{com}.                 //PP: cylce through % marglist to get probability of flips at each margin
.                 foreach marg in $MARGLISTPCT{c -(}
{txt} 36{com}.                         local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 37{com}.                         qui gen swingpct`labelmarg'_`i' = swing_`i'==1 & (vmargin_`i'/pvote)<`marg'
{txt} 38{com}.                         label var swingpct`labelmarg'_`i' "Binary: is vmargin_`i' less than `marg' expressed as pct of turnout"
{txt} 39{com}.                 {c )-}       
{txt} 40{com}.                 foreach marg in $MARGLISTPCT2{c -(}
{txt} 41{com}.                         local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 42{com}.                         qui gen swingpct`labelmarg'2_`i' = swing_`i'==1 & (vmargin_`i'/pvote)<`marg'
{txt} 43{com}.                         label var swingpct`labelmarg'2_`i' "Binary: is vmargin_`i' less than `marg' expressed as pct of turnout"
{txt} 44{com}.                 {c )-}       
{txt} 45{com}.                 
. {c )-}
{txt} 46{com}.         
.         
.         gen closest_flippable_state=.
{txt} 47{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 48{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 49{com}.         qui gen tipping_state = .
{txt} 50{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 51{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 52{com}.         {c )-}
{txt} 53{com}. 
.         //the calculation of min_vmargin assumes *exactly* 2 states are flipped
.         //But here allow that if 1 state flipping takes less votes and flips the election, we want the two state margin lowered
.         gen min_vmarginOR1 = min_vmargin1
{txt} 54{com}.         gen min_vmarginOR2 = min_vmargin2
{txt} 55{com}.         replace min_vmarginOR2 = min_vmargin1 if min_vmargin1<min_vmargin2
{txt} 56{com}.         gen min_vmarginOR3 = min_vmargin3
{txt} 57{com}.         replace min_vmarginOR3 = min_vmargin2 if min_vmargin2<min_vmargin3
{txt} 58{com}.         replace min_vmarginOR3 = min_vmargin1 if min_vmargin1<min_vmarginOR3
{txt} 59{com}.         egen swingers = rowtotal(swing_*)
{txt} 60{com}.         
.         //EC Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 61{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 62{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 63{com}.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 64{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 65{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 66{com}.                 
.                 forvalues statecount=1(1)3{c -(}
{txt} 67{com}.                         qui gen Reversed`statecount'_`marg' = (min_vmarginOR`statecount' <`marg')
{txt} 68{com}.                         qui replace Reversed`statecount'_`marg' = 0 if min_vmarginOR`statecount' ==.
{txt} 69{com}.                 {c )-}
{txt} 70{com}.         {c )-}
{txt} 71{com}.         *drop swing`marg'_*
. 
.         //PP EC Reversals
.         foreach marg in $MARGLISTPCT2{c -(}
{txt} 72{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 73{com}.                 di `labelmarg'
{txt} 74{com}.                 qui egen swingpct`labelmarg'2 = rowtotal(swingpct`labelmarg'2_*)
{txt} 75{com}.                 qui replace swingpct`labelmarg'2 = 0 if swingpct`labelmarg'2 == .
{txt} 76{com}.                 qui gen Reversedpct`labelmarg'2 = (swingpct`labelmarg'2>0 & swingpct`labelmarg'2<51)
{txt} 77{com}.                 qui replace Reversedpct`labelmarg'2 = 0 if swingpct`labelmarg'2 == .
{txt} 78{com}.                 label var Reversedpct`labelmarg'2 "EC reversed by at least one state dropping `marg' pp votes"
{txt} 79{com}. 
.                 forvalues statecount=1(1)3{c -(}
{txt} 80{com}.                         qui gen Reversedpct`statecount'2_`labelmarg' = ((min_vmarginOR`statecount'/pvotes) <`marg')
{txt} 81{com}.                         qui replace Reversedpct`statecount'2_`labelmarg' = 0 if min_vmarginOR`statecount' ==.
{txt} 82{com}.                 {c )-}
{txt} 83{com}.                 
.         {c )-}
{txt} 84{com}.         
.         //PP EC Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt} 85{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 86{com}.                 di `labelmarg'
{txt} 87{com}.                 qui egen swingpct`labelmarg' = rowtotal(swingpct`labelmarg'_*)
{txt} 88{com}.                 qui replace swingpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 89{com}.                 qui gen Reversedpct`labelmarg' = (swingpct`labelmarg'>0 & swingpct`labelmarg'<51)
{txt} 90{com}.                 qui replace Reversedpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 91{com}.                 label var Reversedpct`labelmarg' "EC reversed by at least one state dropping `marg' pp votes"
{txt} 92{com}. 
.                 forvalues statecount=1(1)3{c -(}
{txt} 93{com}.                         qui gen Reversedpct`statecount'_`labelmarg' = ((min_vmarginOR`statecount'/pvotes) <`marg')
{txt} 94{com}.                         qui replace Reversedpct`statecount'_`labelmarg' = 0 if min_vmarginOR`statecount' ==.
{txt} 95{com}.                 {c )-}
{txt} 96{com}.                 
.         {c )-}
{txt} 97{com}.         
.         
.         
.         //NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 98{com}.                 qui gen PVswing`marg'=0
{txt} 99{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt}100{com}.                 
.         {c )-}       
{txt}101{com}. 
.         //PP NPV Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt}102{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt}103{com}.                 qui gen PVswingpct`labelmarg'=0
{txt}104{com}.                 qui replace PVswingpct`labelmarg' = 1 if (margin_votes/pvotes)<`marg'
{txt}105{com}.         {c )-}       
{txt}106{com}.         
.         qui replace swingers = 0 if swingers == .
{txt}107{com}.         qui gen ec_margin = min_vmargin
{txt}108{com}.         qui gen pv_margin = margin_votes
{txt}109{com}.         
.         qui gen zero = swingers == 0
{txt}110{com}.         label var swingers "number of states that could swing election"
{txt}111{com}.         
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt}112{com}.         di `col'
{txt}113{com}.         *di `file'
. 
.         matrix REC[1, `col']            = `file'
{txt}114{com}.         matrix RECPCT[1, `col']         = `file'
{txt}115{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt}116{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt}117{com}.         matrix RPV[1, `col']            = `file'
{txt}118{com}.         matrix RPVPCT[1, `col']         = `file'        
{txt}119{com}.         matrix RECPCT2STATE2[1, `col']          = `file'
{txt}120{com}.         matrix RECPCT3STATE2[1, `col']          = `file'
{txt}121{com}.         
.         local row = 2 //place marglist in rows starting at 2
{txt}122{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}123{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}124{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}125{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}126{com}.                 matrix RPV[`row',1] = `marg'
{txt}127{com}.                 
.                 qui summ Reversed`marg'
{txt}128{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}129{com}.                 qui summ Reversed`marg' if dwin==1
{txt}130{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}131{com}.                 qui summ Reversed`marg' if rwin==1
{txt}132{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}133{com}.                 
.                 qui summ PVswing`marg'
{txt}134{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}135{com}.         
.                 //allocating min votes across 1, 2, 3 states
.                 forvalues statecount=1(1)3{c -(}
{txt}136{com}.                         matrix REC`statecount'STATE[`row',1] = `marg'
{txt}137{com}.                         qui summ Reversed`statecount'_`marg'
{txt}138{com}.                         matrix REC`statecount'STATE[`row',`col'] = r(mean)
{txt}139{com}.                 {c )-}
{txt}140{com}.                 
.                 local row = `row'+1
{txt}141{com}.         {c )-}
{txt}142{com}. 
. 
. 
.         local row = 2 //place marglist in rows starting at 2
{txt}143{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLISTPCT{c -(}
{txt}144{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt}145{com}.                 matrix RECPCT[`row',1] = `marg'
{txt}146{com}.                 matrix RPVPCT[`row',1] = `marg'
{txt}147{com}.                 
.                 qui summ Reversedpct`labelmarg'
{txt}148{com}.                 matrix RECPCT[`row',`col'] = r(mean)
{txt}149{com}.                 qui summ PVswingpct`labelmarg'
{txt}150{com}.                 matrix RPVPCT[`row',`col'] = r(mean)            
{txt}151{com}.                 
.                 //allocating min votes across 1, 2, 3 states
.                 forvalues statecount=1(1)3{c -(}
{txt}152{com}.                         matrix RECPCT`statecount'STATE[`row',1] = `marg'
{txt}153{com}.                         qui summ Reversedpct`statecount'_`labelmarg'
{txt}154{com}.                         matrix RECPCT`statecount'STATE[`row',`col'] = r(mean)
{txt}155{com}.                 {c )-}
{txt}156{com}.                 
.                 local row = `row'+1
{txt}157{com}.         {c )-}
{txt}158{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}159{com}.         //stores fraction of sims reversed by indicated margin
.                 foreach marg in $MARGLISTPCT2{c -(}
{txt}160{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt}161{com}.                 
.                 //allocating min votes across 1, 2, 3 states
.                 forvalues statecount=1(1)3{c -(}
{txt}162{com}.                         matrix RECPCT`statecount'STATE2[`row',1] = `marg'
{txt}163{com}.                         qui summ Reversedpct`statecount'2_`labelmarg'
{txt}164{com}.                         matrix RECPCT`statecount'STATE2[`row',`col'] = r(mean)
{txt}165{com}.                 {c )-}
{txt}166{com}.                 
.                 local row = `row'+1
{txt}167{com}.         {c )-}
{txt}168{com}.         
.         
. ************************************************
. *Outer loop - open datasets
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}169{com}. di "$LOOP"
{txt}170{com}.         
. {c )-}
51
{res}{txt}(100,000 missing values generated)
(48,819 missing values generated)
(25,128 missing values generated)
(16,837 real changes made)
(12,349 missing values generated)
(29,090 real changes made)
(12,855 real changes made)
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
6667
5000
3333
2222
1429
1000
2
3
51
{res}{txt}(40,000 missing values generated)
(15,530 missing values generated)
(5,793 missing values generated)
(7,449 real changes made)
(1,004 missing values generated)
(12,732 real changes made)
(6,778 real changes made)
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
6667
5000
3333
2222
1429
1000
3
4
51
{res}{txt}(40,000 missing values generated)
(17,316 missing values generated)
(8,311 missing values generated)
(8,141 real changes made)
(3,628 missing values generated)
(13,276 real changes made)
(7,058 real changes made)
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
6667
5000
3333
2222
1429
1000
4
5

{com}. 
. global ENDLOOP = $LOOP
{txt}
{com}. 
. cap drop REC*
{txt}
{com}. svmat REC
{txt}
{com}. svmat REC1STATE
{txt}
{com}. svmat REC2STATE
{txt}
{com}. svmat REC3STATE
{txt}
{com}. svmat RECPCT1STATE
{txt}
{com}. svmat RECPCT2STATE
{txt}
{com}. svmat RECPCT3STATE
{txt}
{com}. svmat RECPCT2STATE2
{txt}
{com}. svmat RECPCT3STATE2
{txt}
{com}. cap drop RPV*
{txt}
{com}. svmat RPV
{txt}
{com}. 
. 
. forvalues i=2(1)4{c -(}
{txt}  2{com}. 
.         local file = REC`i' in 1
{txt}  3{com}.         di `file'
{txt}  4{com}.         
. 
.         if `file'==52201{c -(}
{txt}  5{com}.                 local simlabel = "Geruso et al., 2022"
{txt}  6{com}.                 local filelabel = "BGeruso" //52201
{txt}  7{com}.                 local range = .0000001
{txt}  8{com}.         {c )-}
{txt}  9{com}.         if `file'==69006{c -(}
{txt} 10{com}.                 local simlabel = "Gelman et al., 2020"
{txt} 11{com}.                 local filelabel = "AGelman" //69006
{txt} 12{com}.                 local range = .000001
{txt} 13{com}.         {c )-}
{txt} 14{com}.         if `file'==69106{c -(}
{txt} 15{com}.                 local simlabel = "Silver et al., 2020"
{txt} 16{com}.                 local filelabel = "CSilver" //69106
{txt} 17{com}.                 local range = .000001
{txt} 18{com}.         {c )-}
{txt} 19{com}.         
. 
. qui summ REC`i' in 4 
{txt} 20{com}. local ecpostY = r(mean) + .0005
{txt} 21{com}. qui summ REC1 in 4
{txt} 22{com}. local ecpostX = r(mean)
{txt} 23{com}. 
. qui summ RPV`i' in 15 
{txt} 24{com}. local npvpostY = r(mean) - .0005
{txt} 25{com}. qui summ REC1 in 15
{txt} 26{com}. local npvpostX = r(mean)
{txt} 27{com}. 
. 
. local labsize = "med"
{txt} 28{com}. local legsize = "small"
{txt} 29{com}. twoway ///
>         (connected RECPCT1STATE`i' RECPCT1STATE1 in 2/100 if RECPCT1STATE1>0.00001 , lw(thin)   lcolor(black) ms(none) ) ///
>         (connected RECPCT2STATE`i' RECPCT1STATE1 in 2/100 if RECPCT1STATE1>0.00001 , lw(medthick) lcolor(black) ms(none) ) ///
>         (connected RECPCT3STATE`i' RECPCT1STATE1 in 2/100 if RECPCT1STATE1>0.00001 , lw(thick)  lcolor(black) ms(none) ) ///
>         , ///
>         aspect(1) ///
>         xtitle("Margin (Percent of Turnout)", size(`labsize')) ///
>         ytitle("Probability Margin Is Pivotal", size(`labsize')) ///
>         ysc(log r(0.01 0.45)) ///
>         xsc(log r(.000025 .001)) ///
>         ylab( 0.02500 "2.5%" 0.0500 "5.0%" 0.1000 "10.0%" 0.2500 "25.0%" , nogrid angle(horizontal) labsize(`labsize')) ///
>         xlab( .00003 `"".003%" "4,500 Votes""'  0.0001 `"".01%" "15,000""' 0.0003 `"".03%" "45,000""'  0.001 `"".1%" "150,000""' , nogrid angle(horizontal) labsize(`labsize')) ///
>         graphr(c(white) lc(white)) ///
>         name(multi_detail_`filelabel', replace) ///
>         xline(0.000598, lcolor(gs11) lwidth(thin) lpattern(solid) ) /// 0.000598 = 40,865/68,329,141 in 1960 in NJ, IL, MO
>         xline(0.000320, lcolor(gs11) lwidth(thin) lpattern(solid) ) ///  0.000320 = 25,579/79,980,515   in 1976 in MI and OH
>         xline(0.0006034, lcolor(gs11) lwidth(thin) lpattern(solid) ) /// 0.0006034 = 77744/128,839,000
>         xline(0.000276, lcolor(gs11) lwidth(thin) lpattern(solid) ) ///  0.0002760 = 42918/155,484,000
>         text(.05 0.000265 " 3 States "  "2020 ", color(gs11) place(w) justification(right) size(`legsize')) ///
>         text(.033 0.000320 " 2 States"  " 1976", color(gs11) place(e) justification(left) size(`legsize')) ///
>         text(.025 .0006094 " 3 States" " 1960 &" " 2016", color(gs11) place(e) justification(left) size(`legsize')) ///
>         legend(order(1 "1 State" 2 "2 States" 3 "3 States") title("Votes Divided Betweeen", size(`legsize')) position(10) col(1) ring(0) region(lwidth(thin)) size(`legsize'))  
{txt} 30{com}.         
.         graph export "figures`c(dirsep)'FigureA2_`filelabel'.pdf", replace
{txt} 31{com}. 
. {c )-}
52201
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{res}{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA2_BGeruso.pdf written in PDF format)
69006
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{res}{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA2_AGelman.pdf written in PDF format)
69106
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
gsize,  default attributes used)
{p_end}
{res}{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA2_CSilver.pdf written in PDF format)

{com}.         
.         
. ************************************************
. *Reference Tables
. ************************************************        
. 
. 
. //Panel B of Table 2
. matrix list RECPCT2STATE2
{res}
{txt}RECPCT2STATE2[30,10]
            c1         c2         c3         c4         c5         c6         c7
 r1 {res}         .      52201      69006      69106          .          .          .
{txt} r2 {res} 1.000e-07     .00009     .00175    .000725          .          .          .
{txt} r3 {res} 2.000e-07     .00017     .00175    .000725          .          .          .
{txt} r4 {res} 5.000e-07     .00033     .00175    .000725          .          .          .
{txt} r5 {res} 1.000e-06     .00066     .00175     .00095          .          .          .
{txt} r6 {res} 2.000e-06     .00136     .00195    .001325          .          .          .
{txt} r7 {res} 5.000e-06     .00316    .003425     .00375          .          .          .
{txt} r8 {res}    .00001     .00638    .007125    .007425          .          .          .
{txt} r9 {res}    .00002     .01256      .0123    .014325          .          .          .
{txt}r10 {res}    .00005     .03207    .029575     .03395          .          .          .
{txt}r11 {res}     .0001      .0635    .058775    .064925          .          .          .
{txt}r12 {res}     .0002     .11385    .109175    .117625          .          .          .
{txt}r13 {res}     .0005     .20972       .221    .228425          .          .          .
{txt}r14 {res}      .001     .28865    .323475    .332375          .          .          .
{txt}r15 {res}         .          .          .          .          .          .          .
{txt}r16 {res}         .          .          .          .          .          .          .
{txt}r17 {res}         .          .          .          .          .          .          .
{txt}r18 {res}         .          .          .          .          .          .          .
{txt}r19 {res}         .          .          .          .          .          .          .
{txt}r20 {res}         .          .          .          .          .          .          .
{txt}r21 {res}         .          .          .          .          .          .          .
{txt}r22 {res}         .          .          .          .          .          .          .
{txt}r23 {res}         .          .          .          .          .          .          .
{txt}r24 {res}         .          .          .          .          .          .          .
{txt}r25 {res}         .          .          .          .          .          .          .
{txt}r26 {res}         .          .          .          .          .          .          .
{txt}r27 {res}         .          .          .          .          .          .          .
{txt}r28 {res}         .          .          .          .          .          .          .
{txt}r29 {res}         .          .          .          .          .          .          .
{txt}r30 {res}         .          .          .          .          .          .          .

{txt}            c8         c9        c10
 r1 {res}         .          .          .
{txt} r2 {res}         .          .          .
{txt} r3 {res}         .          .          .
{txt} r4 {res}         .          .          .
{txt} r5 {res}         .          .          .
{txt} r6 {res}         .          .          .
{txt} r7 {res}         .          .          .
{txt} r8 {res}         .          .          .
{txt} r9 {res}         .          .          .
{txt}r10 {res}         .          .          .
{txt}r11 {res}         .          .          .
{txt}r12 {res}         .          .          .
{txt}r13 {res}         .          .          .
{txt}r14 {res}         .          .          .
{txt}r15 {res}         .          .          .
{txt}r16 {res}         .          .          .
{txt}r17 {res}         .          .          .
{txt}r18 {res}         .          .          .
{txt}r19 {res}         .          .          .
{txt}r20 {res}         .          .          .
{txt}r21 {res}         .          .          .
{txt}r22 {res}         .          .          .
{txt}r23 {res}         .          .          .
{txt}r24 {res}         .          .          .
{txt}r25 {res}         .          .          .
{txt}r26 {res}         .          .          .
{txt}r27 {res}         .          .          .
{txt}r28 {res}         .          .          .
{txt}r29 {res}         .          .          .
{txt}r30 {res}         .          .          .
{reset}
{com}. 
. matrix col1     =RECPCT2STATE2[2..15,1]
{txt}
{com}. matrix col2 = col1*150000000
{txt}
{com}. matrix col3     =RECPCT2STATE2[2..15,3]
{txt}
{com}. matrix col4     =RECPCT2STATE2[2..15,2]
{txt}
{com}. matrix col5     =RECPCT2STATE2[2..15,4]
{txt}
{com}. 
. //binding low resolution in Silver, Gelman due to truncation of source data
. forvalues i=1(1)4{c -(}
{txt}  2{com}.         matrix col5[`i',1]=.
{txt}  3{com}.         matrix col3[`i',1]=.
{txt}  4{com}. {c )-}
{txt}
{com}. matrix PANELB = (col1,col2,col3,col4,col5)
{txt}
{com}. matrix colnames PANELB = percent votesif150M Gelman Geruso Silver
{txt}
{com}. matrix list PANELB
{res}
{txt}PANELB[14,5]
         percent  votesif150M       Gelman       Geruso       Silver
 r2 {res}   1.000e-07           15            .       .00009            .
{txt} r3 {res}   2.000e-07           30            .       .00017            .
{txt} r4 {res}   5.000e-07           75            .       .00033            .
{txt} r5 {res}   1.000e-06          150            .       .00066            .
{txt} r6 {res}   2.000e-06          300       .00195       .00136      .001325
{txt} r7 {res}   5.000e-06          750      .003425       .00316       .00375
{txt} r8 {res}      .00001         1500      .007125       .00638      .007425
{txt} r9 {res}      .00002         3000        .0123       .01256      .014325
{txt}r10 {res}      .00005         7500      .029575       .03207       .03395
{txt}r11 {res}       .0001        15000      .058775        .0635      .064925
{txt}r12 {res}       .0002        30000      .109175       .11385      .117625
{txt}r13 {res}       .0005        75000         .221       .20972      .228425
{txt}r14 {res}        .001       150000      .323475       .28865      .332375
{txt}r15 {res}           .            .            .            .            .
{reset}
{com}. 
. xml_tab PANELB, save("tables`c(dirsep)'Table1b") replace        


{txt}note: results saved to tables/Table1b.xml

{com}.         
. 
. //Panel C of Table 2
. matrix list RECPCT3STATE2
{res}
{txt}RECPCT3STATE2[30,10]
            c1         c2         c3         c4         c5         c6         c7
 r1 {res}         .      52201      69006      69106          .          .          .
{txt} r2 {res} 1.000e-07     .00009     .00175    .000725          .          .          .
{txt} r3 {res} 2.000e-07     .00017     .00175    .000725          .          .          .
{txt} r4 {res} 5.000e-07     .00033     .00175    .000725          .          .          .
{txt} r5 {res} 1.000e-06     .00066     .00175     .00095          .          .          .
{txt} r6 {res} 2.000e-06     .00136     .00195    .001325          .          .          .
{txt} r7 {res} 5.000e-06     .00316    .003425     .00375          .          .          .
{txt} r8 {res}    .00001     .00638    .007125    .007425          .          .          .
{txt} r9 {res}    .00002     .01257      .0123    .014325          .          .          .
{txt}r10 {res}    .00005     .03238     .02965      .0341          .          .          .
{txt}r11 {res}     .0001     .06467    .059075      .0657          .          .          .
{txt}r12 {res}     .0002     .11885    .111525    .121725          .          .          .
{txt}r13 {res}     .0005     .23594     .23825     .24745          .          .          .
{txt}r14 {res}      .001     .34686    .375425    .385275          .          .          .
{txt}r15 {res}         .          .          .          .          .          .          .
{txt}r16 {res}         .          .          .          .          .          .          .
{txt}r17 {res}         .          .          .          .          .          .          .
{txt}r18 {res}         .          .          .          .          .          .          .
{txt}r19 {res}         .          .          .          .          .          .          .
{txt}r20 {res}         .          .          .          .          .          .          .
{txt}r21 {res}         .          .          .          .          .          .          .
{txt}r22 {res}         .          .          .          .          .          .          .
{txt}r23 {res}         .          .          .          .          .          .          .
{txt}r24 {res}         .          .          .          .          .          .          .
{txt}r25 {res}         .          .          .          .          .          .          .
{txt}r26 {res}         .          .          .          .          .          .          .
{txt}r27 {res}         .          .          .          .          .          .          .
{txt}r28 {res}         .          .          .          .          .          .          .
{txt}r29 {res}         .          .          .          .          .          .          .
{txt}r30 {res}         .          .          .          .          .          .          .

{txt}            c8         c9        c10
 r1 {res}         .          .          .
{txt} r2 {res}         .          .          .
{txt} r3 {res}         .          .          .
{txt} r4 {res}         .          .          .
{txt} r5 {res}         .          .          .
{txt} r6 {res}         .          .          .
{txt} r7 {res}         .          .          .
{txt} r8 {res}         .          .          .
{txt} r9 {res}         .          .          .
{txt}r10 {res}         .          .          .
{txt}r11 {res}         .          .          .
{txt}r12 {res}         .          .          .
{txt}r13 {res}         .          .          .
{txt}r14 {res}         .          .          .
{txt}r15 {res}         .          .          .
{txt}r16 {res}         .          .          .
{txt}r17 {res}         .          .          .
{txt}r18 {res}         .          .          .
{txt}r19 {res}         .          .          .
{txt}r20 {res}         .          .          .
{txt}r21 {res}         .          .          .
{txt}r22 {res}         .          .          .
{txt}r23 {res}         .          .          .
{txt}r24 {res}         .          .          .
{txt}r25 {res}         .          .          .
{txt}r26 {res}         .          .          .
{txt}r27 {res}         .          .          .
{txt}r28 {res}         .          .          .
{txt}r29 {res}         .          .          .
{txt}r30 {res}         .          .          .
{reset}
{com}. 
. matrix col1     =RECPCT3STATE2[2..15,1]
{txt}
{com}. matrix col2 = col1*150000000
{txt}
{com}. matrix col3     =RECPCT3STATE2[2..15,3]
{txt}
{com}. matrix col4     =RECPCT3STATE2[2..15,2]
{txt}
{com}. matrix col5     =RECPCT3STATE2[2..15,4]
{txt}
{com}. 
. //binding low resolution in Silver, Gelman due to truncation of source data
. forvalues i=1(1)4{c -(}
{txt}  2{com}.         matrix col5[`i',1]=.
{txt}  3{com}.         matrix col3[`i',1]=.
{txt}  4{com}. {c )-}
{txt}
{com}. matrix PANELC = (col1,col2,col3,col4,col5)
{txt}
{com}. matrix colnames PANELC = percent votesif150M Gelman Geruso Silver
{txt}
{com}. matrix list PANELC
{res}
{txt}PANELC[14,5]
         percent  votesif150M       Gelman       Geruso       Silver
 r2 {res}   1.000e-07           15            .       .00009            .
{txt} r3 {res}   2.000e-07           30            .       .00017            .
{txt} r4 {res}   5.000e-07           75            .       .00033            .
{txt} r5 {res}   1.000e-06          150            .       .00066            .
{txt} r6 {res}   2.000e-06          300       .00195       .00136      .001325
{txt} r7 {res}   5.000e-06          750      .003425       .00316       .00375
{txt} r8 {res}      .00001         1500      .007125       .00638      .007425
{txt} r9 {res}      .00002         3000        .0123       .01257      .014325
{txt}r10 {res}      .00005         7500       .02965       .03238        .0341
{txt}r11 {res}       .0001        15000      .059075       .06467        .0657
{txt}r12 {res}       .0002        30000      .111525       .11885      .121725
{txt}r13 {res}       .0005        75000       .23825       .23594       .24745
{txt}r14 {res}        .001       150000      .375425       .34686      .385275
{txt}r15 {res}           .            .            .            .            .
{reset}
{com}. 
. xml_tab PANELC, save("tables`c(dirsep)'Table1c") replace        


{txt}note: results saved to tables/Table1c.xml

{com}. 
{txt}end of do-file

{com}. 
. *Figure A3 (Panel A), Table A1
. do do_files`c(dirsep)'FigureA3TableA1.do
{txt}
{com}. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"
{txt}
{com}. 
. global LIST = "52201 52204 52210 52211  22202 12201" //M1; M12 (1964-2016); 52210 is 1936-1956; 52211 is 1916-1932;  R1; A1
{txt}
{com}. 
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates;                                           columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECPCT    =J(30, 10, .)           //table of summary of reversal rates based on % margins;        columns are models, rows are %critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on R EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define RPVPCT    =J(30, 10, .)           //table of summary of NPV reversal rates; in % margins (pp)
{txt}
{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 15{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 16{com}.         {c )-}
{txt} 17{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 18{com}.         qui gen pvshare = rvotes/pvotes
{txt} 19{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 20{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 21{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 22{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 23{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 24{com}.         {c )-}
{txt} 25{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 26{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 27{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 28{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 29{com}.         qui gen cf_revn_total  = revn_total
{txt} 30{com}.         qui gen cf_rwin = rwin
{txt} 31{com}. 
.         qui gen inv=0
{txt} 32{com}.         qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)
{txt} 33{com}.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 34{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 35{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 36{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 37{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim (nationally)
{txt} 38{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim (nationally)
{txt} 39{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 40{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 41{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 42{com}.                 label var vmargin_`i' "The margin in vote counts in state `i', if `i' could flip EC"
{txt} 43{com}.                 
.                 //Levels: cylce through marglist to get probability of flips at each margin
.                 foreach marg in $MARGLIST{c -(}
{txt} 44{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 45{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 46{com}.                 {c )-}
{txt} 47{com}.                 //PP: cylce through % marglist to get probability of flips at each margin
.                 foreach marg in $MARGLISTPCT{c -(}
{txt} 48{com}.                         local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 49{com}.                         qui gen swingpct`labelmarg'_`i' = swing_`i'==1 & (vmargin_`i'/pvote)<`marg'
{txt} 50{com}.                         label var swingpct`labelmarg'_`i' "Binary: is vmargin_`i' less than `marg' expressed as pct of turnout"
{txt} 51{com}.                 {c )-}       
{txt} 52{com}.                 
.         {c )-}
{txt} 53{com}.         
.         qui gen closest_flippable_state=.
{txt} 54{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 55{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 56{com}.         qui gen tipping_state = .
{txt} 57{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 58{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 59{com}.         {c )-}
{txt} 60{com}.         qui egen swingers = rowtotal(swing_*)
{txt} 61{com}.         
.         //Levels EC Reversals 
.         foreach marg in $MARGLIST{c -(}
{txt} 62{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 63{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 64{com}.                 *qui gen notReversed`marg' = swing`marg'==0
.                 *label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 65{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 66{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 67{com}.         {c )-}
{txt} 68{com}.                 
.         //PP EC Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt} 69{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 70{com}.                 di `labelmarg'
{txt} 71{com}.                 qui egen swingpct`labelmarg' = rowtotal(swingpct`labelmarg'_*)
{txt} 72{com}.                 qui replace swingpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 73{com}.                 *qui gen notReversed`marg' = swing`marg'==0
.                 *label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
.                 qui gen Reversedpct`labelmarg' = (swingpct`labelmarg'>0 & swingpct`labelmarg'<51)
{txt} 74{com}.                 qui replace Reversedpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 75{com}.                 label var Reversedpct`labelmarg' "EC reversed by at least one state dropping `marg' pp votes"
{txt} 76{com}.         {c )-}
{txt} 77{com}.         
.         //Levels NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 78{com}.                 qui gen PVswing`marg'=0
{txt} 79{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt} 80{com}.         {c )-}
{txt} 81{com}.         //PP NPV Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt} 82{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 83{com}.                 qui gen PVswingpct`labelmarg'=0
{txt} 84{com}.                 qui replace PVswingpct`labelmarg' = 1 if (margin_votes/pvotes)<`marg'
{txt} 85{com}.         {c )-}       
{txt} 86{com}. 
.         qui replace swingers = 0 if swingers == .
{txt} 87{com}.         qui gen ec_margin = min_vmargin
{txt} 88{com}.         qui gen pv_margin = margin_votes
{txt} 89{com}.         
.         qui gen zero = swingers == 0
{txt} 90{com}.         label var swingers "number of states that could swing election"
{txt} 91{com}. 
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt} 92{com}.         di `col'
{txt} 93{com}.         di `file'
{txt} 94{com}.         matrix REC[1, `col']            = `file'
{txt} 95{com}.         matrix RECPCT[1, `col']         = `file'
{txt} 96{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt} 97{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt} 98{com}.         matrix RPV[1, `col']            = `file'
{txt} 99{com}.         matrix RPVPCT[1, `col']         = `file'        
{txt}100{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}101{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}102{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}103{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}104{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}105{com}.                 matrix RPV[`row',1] = `marg'
{txt}106{com}.                 
.                 qui summ Reversed`marg'
{txt}107{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}108{com}.                 qui summ Reversed`marg' if dwin==1
{txt}109{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}110{com}.                 qui summ Reversed`marg' if rwin==1
{txt}111{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}112{com}.                 
.                 qui summ PVswing`marg'
{txt}113{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}114{com}.                 
.                 local row = `row'+1
{txt}115{com}.         {c )-}
{txt}116{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}117{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLISTPCT{c -(}
{txt}118{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt}119{com}.                 matrix RECPCT[`row',1] = `marg'
{txt}120{com}.                 matrix RPVPCT[`row',1] = `marg'
{txt}121{com}.                 
.                 summ Reversedpct`labelmarg'
{txt}122{com}.                 matrix RECPCT[`row',`col'] = r(mean)
{txt}123{com}.                 qui summ PVswingpct`labelmarg'
{txt}124{com}.                 matrix RPVPCT[`row',`col'] = r(mean)            
{txt}125{com}.                 
.                 local row = `row'+1
{txt}126{com}.         {c )-}
{txt}127{com}.         
.         
. ************************************************
. *Outer loop - open datasets
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}128{com}. di "$LOOP"
{txt}129{com}.         
. {c )-}
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
2
52201

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rev~10000000 {c |}{res}    100,000      .00009    .0094865          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~5000000 {c |}{res}    100,000      .00017    .0130374          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~2000000 {c |}{res}    100,000      .00033     .018163          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~1000000 {c |}{res}    100,000      .00066    .0256821          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~500000 {c |}{res}    100,000      .00136    .0368533          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~200000 {c |}{res}    100,000      .00314    .0559479          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~t100000 {c |}{res}    100,000      .00628    .0789976          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t50000 {c |}{res}    100,000      .01208    .1092437          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~20000 {c |}{res}    100,000      .02889    .1674982          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t10000 {c |}{res}    100,000      .05251    .2230542          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t5000 {c |}{res}    100,000      .08439    .2779732          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reverse~2000 {c |}{res}    100,000      .13472     .341426          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t1000 {c |}{res}    100,000      .17543    .3803364          0          1
3
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
3
52204

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rev~10000000 {c |}{res}    100,000      .00004    .0063245          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~5000000 {c |}{res}    100,000      .00009    .0094865          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~2000000 {c |}{res}    100,000      .00019    .0137828          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~1000000 {c |}{res}    100,000      .00035    .0187051          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~500000 {c |}{res}    100,000       .0007    .0264484          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~200000 {c |}{res}    100,000      .00167    .0408317          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~t100000 {c |}{res}    100,000      .00338    .0580397          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t50000 {c |}{res}    100,000      .00641    .0798059          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~20000 {c |}{res}    100,000      .01485     .120953          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t10000 {c |}{res}    100,000      .02589    .1588079          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t5000 {c |}{res}    100,000      .03982    .1955371          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reverse~2000 {c |}{res}    100,000      .06306    .2430721          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t1000 {c |}{res}    100,000      .08254     .275187          0          1
4
48
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
4
52210

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rev~10000000 {c |}{res}    100,000      .00001    .0031623          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~5000000 {c |}{res}    100,000      .00002    .0044721          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~2000000 {c |}{res}    100,000       .0001    .0099995          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~1000000 {c |}{res}    100,000      .00018    .0134153          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~500000 {c |}{res}    100,000      .00043    .0207321          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~200000 {c |}{res}    100,000      .00107    .0326935          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~t100000 {c |}{res}    100,000      .00207    .0454504          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t50000 {c |}{res}    100,000      .00384     .061849          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~20000 {c |}{res}    100,000      .00864    .0925496          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t10000 {c |}{res}    100,000      .01524    .1225067          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t5000 {c |}{res}    100,000      .02385    .1525825          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reverse~2000 {c |}{res}    100,000      .03725    .1893748          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t1000 {c |}{res}    100,000      .05029    .2185438          0          1
5
48
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
5
52211

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rev~10000000 {c |}{res}    100,000      .00002    .0044721          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~5000000 {c |}{res}    100,000      .00002    .0044721          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~2000000 {c |}{res}    100,000      .00011    .0104876          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~1000000 {c |}{res}    100,000       .0003     .017318          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~500000 {c |}{res}    100,000      .00044    .0209717          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~200000 {c |}{res}    100,000      .00096    .0309691          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~t100000 {c |}{res}    100,000      .00181    .0425058          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t50000 {c |}{res}    100,000      .00367    .0604696          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~20000 {c |}{res}    100,000      .00851    .0918568          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t10000 {c |}{res}    100,000      .01513    .1220706          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t5000 {c |}{res}    100,000      .02317    .1504439          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reverse~2000 {c |}{res}    100,000       .0364    .1872843          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t1000 {c |}{res}    100,000      .04903    .2159318          0          1
6
38
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
6
22202

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rev~10000000 {c |}{res}    100,000      .00003    .0054772          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~5000000 {c |}{res}    100,000      .00009    .0094865          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~2000000 {c |}{res}    100,000      .00025    .0158095          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~1000000 {c |}{res}    100,000      .00056    .0236578          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~500000 {c |}{res}    100,000      .00115    .0338923          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~200000 {c |}{res}    100,000       .0027    .0518916          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~t100000 {c |}{res}    100,000      .00528    .0724719          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t50000 {c |}{res}    100,000      .01006    .0997943          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~20000 {c |}{res}    100,000      .02357    .1517059          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t10000 {c |}{res}    100,000      .04242    .2015464          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t5000 {c |}{res}    100,000      .06884    .2531831          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reverse~2000 {c |}{res}    100,000      .11276    .3163008          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t1000 {c |}{res}    100,000      .14933    .3564152          0          1
7
30
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
7
12201

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rev~10000000 {c |}{res}    100,000      .00003    .0054772          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~5000000 {c |}{res}    100,000      .00007    .0083663          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~2000000 {c |}{res}    100,000      .00017    .0130374          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~1000000 {c |}{res}    100,000      .00044    .0209717          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~500000 {c |}{res}    100,000      .00082     .028624          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~200000 {c |}{res}    100,000      .00226    .0474859          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~t100000 {c |}{res}    100,000      .00444    .0664856          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t50000 {c |}{res}    100,000      .00831    .0907801          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~20000 {c |}{res}    100,000      .01958    .1385526          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t10000 {c |}{res}    100,000      .03602    .1863408          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t5000 {c |}{res}    100,000      .06068    .2387436          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reverse~2000 {c |}{res}    100,000      .10372    .3048985          0          1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t1000 {c |}{res}    100,000      .14084    .3478582          0          1
8
{txt}
{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. cap drop REC*
{txt}
{com}. svmat REC
{txt}
{com}. cap drop RPV*
{txt}
{com}. svmat RPV
{txt}
{com}. cap drop RECPCT*
{txt}
{com}. svmat RECPCT
{txt}
{com}. cap drop RPVPCT*
{txt}
{com}. svmat RPVPCT
{txt}
{com}. 
. 
. ************************************************
. *Reorganize output to hold margin fixed and examine changes by model year
. ************************************************
. //First column is date, subsequent columns are margin, with cells being percent within margin
. //Pickout .0001%, .001, .01, .1
. 
. // 1988-2016 (2002)     1964-2016(1990) 1936-1956 (1946)        1916-1932 (1924)        1872-1888 (1880)        1836-1852 (1844)
. // 52201                52204                   52210                           52211                           22202                           12201
.         
. matrix H=J(7,5, .)
{txt}
{com}. matrix H[2,1] = 1844
{txt}
{com}. matrix H[3,1] = 1880
{txt}
{com}. matrix H[4,1] = 1924
{txt}
{com}. matrix H[5,1] = 1946
{txt}
{com}. matrix H[6,1] = 1990
{txt}
{com}. matrix H[7,1] = 2002
{txt}
{com}. 
. matrix H[1,2] = .0001
{txt}
{com}. matrix H[1,3] = .001
{txt}
{com}. matrix H[1,4] = .01
{txt}
{com}. matrix H[1,5] = .1
{txt}
{com}. 
. forvalues i=2(1)7{c -(}
{txt}  2{com}.         local row = 9 - `i'
{txt}  3{com}.         matrix H[`row',2]= RECPCT[5,`i'] 
{txt}  4{com}.         matrix H[`row',3]= RECPCT[8,`i']        
{txt}  5{com}.         matrix H[`row',4]= RECPCT[11,`i']       
{txt}  6{com}.         matrix H[`row',5]= RECPCT[14,`i']       
{txt}  7{com}. {c )-}
{txt}
{com}. matrix list H
{res}
{txt}H[7,5]
        c1      c2      c3      c4      c5
r1 {res}      .   .0001    .001     .01      .1
{txt}r2 {res}   1844  .00044  .00444  .03602  .14084
{txt}r3 {res}   1880  .00056  .00528  .04242  .14933
{txt}r4 {res}   1924   .0003  .00181  .01513  .04903
{txt}r5 {res}   1946  .00018  .00207  .01524  .05029
{txt}r6 {res}   1990  .00035  .00338  .02589  .08254
{txt}r7 {res}   2002  .00066  .00628  .05251  .17543
{reset}
{com}. cap drop H*
{txt}
{com}. svmat H
{txt}
{com}. 
. local labsize = "medsmall"
{txt}
{com}. twoway ///
>         (connected H2 H1 in 2/7 , lw(thin)              lcolor(gs8) ms(none) ) ///
>                 (connected H3 H1 in 2/7 , lw(med)               lcolor(gs8) ms(none) ) ///
>                         (connected H4 H1 in 2/7 , lw(medium)            lcolor(black) ms(none) ) ///
>                                 (connected H5 H1 in 2/7 , lw(thick)             lcolor(black) ms(none) ) ///
>         , ///
>         aspect(.5) ///
>         xtitle("", size(`labsize')) ///
>         ytitle("Probability that Indicated Margin Is Pivotal", size(`labsize')) ///
>         ysc(log r(.0001 .001)) ///
>         ylab(0.0001 ".01%" 0.0005 ".05%" 0.0010 ".10%" 0.0050 "0.5%" 0.0100 "1.0%" 0.0500 "5.0%" 0.1000 "10.0%" , nogrid angle(horizontal) labsize(`labsize')) ///
>         xlab(1844 "1836-1852"  1880 "1872-1888"  1924 "1916-1932"  1946 "1936-1956" 1990 "1964-2016" 2002 "1988-2016",  angle(45) labsize(`labsize')) ///
>         graphr(c(white) lc(white)) ///
>         name(prob_historyPCT, replace) ///
>         legend(title("Margin", size(small)) order( 4 ".1 points" 3 ".01 points" 2 ".001 points" 1 ".0001 points" )region(lwidth(thin)) col(4) size(small))  
{p 0 4 2}
{txt}(note:  named style
med not found in class
linewidth,  default attributes used)
{p_end}
{com}{txt}{p 0 8} (note:  linewidth  {com}{txt}not found in scheme, default attributes used){p_end}
{p 0 4 2}
{txt}(note:  named style
med not found in class
linewidth,  default attributes used)
{p_end}
{com}{txt}{p 0 8} (note:  linewidth  {com}{txt}not found in scheme, default attributes used){p_end}
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'FigureA3a.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA3a.pdf written in PDF format)

{com}. 
. ************************************************
. *Reference Tables
. ************************************************        
.         
. 
. 
. //Table A1
. matrix list RECPCT
{res}
{txt}RECPCT[30,10]
            c1         c2         c3         c4         c5         c6         c7
 r1 {res}         .      52201      52204      52210      52211      22202      12201
{txt} r2 {res} 1.000e-07     .00009     .00004     .00001     .00002     .00003     .00003
{txt} r3 {res} 2.000e-07     .00017     .00009     .00002     .00002     .00009     .00007
{txt} r4 {res} 5.000e-07     .00033     .00019      .0001     .00011     .00025     .00017
{txt} r5 {res} 1.000e-06     .00066     .00035     .00018      .0003     .00056     .00044
{txt} r6 {res} 2.000e-06     .00136      .0007     .00043     .00044     .00115     .00082
{txt} r7 {res} 5.000e-06     .00314     .00167     .00107     .00096      .0027     .00226
{txt} r8 {res}    .00001     .00628     .00338     .00207     .00181     .00528     .00444
{txt} r9 {res}    .00002     .01208     .00641     .00384     .00367     .01006     .00831
{txt}r10 {res}    .00005     .02889     .01485     .00864     .00851     .02357     .01958
{txt}r11 {res}     .0001     .05251     .02589     .01524     .01513     .04242     .03602
{txt}r12 {res}     .0002     .08439     .03982     .02385     .02317     .06884     .06068
{txt}r13 {res}     .0005     .13472     .06306     .03725      .0364     .11276     .10372
{txt}r14 {res}      .001     .17543     .08254     .05029     .04903     .14933     .14084
{txt}r15 {res}         .          .          .          .          .          .          .
{txt}r16 {res}         .          .          .          .          .          .          .
{txt}r17 {res}         .          .          .          .          .          .          .
{txt}r18 {res}         .          .          .          .          .          .          .
{txt}r19 {res}         .          .          .          .          .          .          .
{txt}r20 {res}         .          .          .          .          .          .          .
{txt}r21 {res}         .          .          .          .          .          .          .
{txt}r22 {res}         .          .          .          .          .          .          .
{txt}r23 {res}         .          .          .          .          .          .          .
{txt}r24 {res}         .          .          .          .          .          .          .
{txt}r25 {res}         .          .          .          .          .          .          .
{txt}r26 {res}         .          .          .          .          .          .          .
{txt}r27 {res}         .          .          .          .          .          .          .
{txt}r28 {res}         .          .          .          .          .          .          .
{txt}r29 {res}         .          .          .          .          .          .          .
{txt}r30 {res}         .          .          .          .          .          .          .

{txt}            c8         c9        c10
 r1 {res}         .          .          .
{txt} r2 {res}         .          .          .
{txt} r3 {res}         .          .          .
{txt} r4 {res}         .          .          .
{txt} r5 {res}         .          .          .
{txt} r6 {res}         .          .          .
{txt} r7 {res}         .          .          .
{txt} r8 {res}         .          .          .
{txt} r9 {res}         .          .          .
{txt}r10 {res}         .          .          .
{txt}r11 {res}         .          .          .
{txt}r12 {res}         .          .          .
{txt}r13 {res}         .          .          .
{txt}r14 {res}         .          .          .
{txt}r15 {res}         .          .          .
{txt}r16 {res}         .          .          .
{txt}r17 {res}         .          .          .
{txt}r18 {res}         .          .          .
{txt}r19 {res}         .          .          .
{txt}r20 {res}         .          .          .
{txt}r21 {res}         .          .          .
{txt}r22 {res}         .          .          .
{txt}r23 {res}         .          .          .
{txt}r24 {res}         .          .          .
{txt}r25 {res}         .          .          .
{txt}r26 {res}         .          .          .
{txt}r27 {res}         .          .          .
{txt}r28 {res}         .          .          .
{txt}r29 {res}         .          .          .
{txt}r30 {res}         .          .          .
{reset}
{com}. 
. matrix col1     =RECPCT[2..15,1]
{txt}
{com}. matrix col2 = col1*150000000
{txt}
{com}. matrix col4 = col1*60000000
{txt}
{com}. matrix col6 = col1*40000000
{txt}
{com}. matrix col8 = col1*10000000
{txt}
{com}. matrix col10 = col1*3000000
{txt}
{com}. 
. matrix col3     =RECPCT[2..15,3]
{txt}
{com}. matrix col5     =RECPCT[2..15,4]
{txt}
{com}. matrix col7     =RECPCT[2..15,5]
{txt}
{com}. matrix col9     =RECPCT[2..15,6]
{txt}
{com}. matrix col11=RECPCT[2..15,7]
{txt}
{com}. 
. matrix PANELA = (col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11)
{txt}
{com}. matrix colnames PANELA = percent votesif150M 1964 votesif60M 1936 votesif40M 1916 votesif10M 1872 votesif30M 1836   
{txt}
{com}. matrix list PANELA
{res}
{txt}PANELA[14,11]
         percent  votesif150M         1964   votesif60M         1936   votesif40M
 r2 {res}   1.000e-07           15       .00004            6       .00001            4
{txt} r3 {res}   2.000e-07           30       .00009           12       .00002            8
{txt} r4 {res}   5.000e-07           75       .00019           30        .0001           20
{txt} r5 {res}   1.000e-06          150       .00035           60       .00018           40
{txt} r6 {res}   2.000e-06          300        .0007          120       .00043           80
{txt} r7 {res}   5.000e-06          750       .00167          300       .00107          200
{txt} r8 {res}      .00001         1500       .00338          600       .00207          400
{txt} r9 {res}      .00002         3000       .00641         1200       .00384          800
{txt}r10 {res}      .00005         7500       .01485         3000       .00864         2000
{txt}r11 {res}       .0001        15000       .02589         6000       .01524         4000
{txt}r12 {res}       .0002        30000       .03982        12000       .02385         8000
{txt}r13 {res}       .0005        75000       .06306        30000       .03725        20000
{txt}r14 {res}        .001       150000       .08254        60000       .05029        40000
{txt}r15 {res}           .            .            .            .            .            .

{txt}            1916   votesif10M         1872   votesif30M         1836
 r2 {res}      .00002            1       .00003           .3       .00003
{txt} r3 {res}      .00002            2       .00009           .6       .00007
{txt} r4 {res}      .00011            5       .00025          1.5       .00017
{txt} r5 {res}       .0003           10       .00056            3       .00044
{txt} r6 {res}      .00044           20       .00115            6       .00082
{txt} r7 {res}      .00096           50        .0027           15       .00226
{txt} r8 {res}      .00181          100       .00528           30       .00444
{txt} r9 {res}      .00367          200       .01006           60       .00831
{txt}r10 {res}      .00851          500       .02357          150       .01958
{txt}r11 {res}      .01513         1000       .04242          300       .03602
{txt}r12 {res}      .02317         2000       .06884          600       .06068
{txt}r13 {res}       .0364         5000       .11276         1500       .10372
{txt}r14 {res}      .04903        10000       .14933         3000       .14084
{txt}r15 {res}           .            .            .            .            .
{reset}
{com}. 
. xml_tab PANELA, save("tables`c(dirsep)'TableA1") replace        


{txt}note: results saved to tables/TableA1.xml

{com}.         
. 
{txt}end of do-file

{com}. 
. *Figure A3 (Panel B)
. do do_files`c(dirsep)'FigureA3b.do
{txt}
{com}. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. ************************************************
. *HISTORICAL VERSION
. *
. *
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"
{txt}
{com}. 
. global LIST = "52201 52204 52210 52211 22202 12201" //M1; M12 (1964-2016); 52210 is 1936-1956; 52211 is 1916-1932;  R1; A1
{txt}
{com}. 
. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
.         
. ************************************************
. *Make vars
. ************************************************        
. 
.         if `file'==52201{c -(}
{txt}  4{com}.                 local simlabel = "1988-2016"
{txt}  5{com}.                 local range = 538
{txt}  6{com}.                 local xline = 270
{txt}  7{com}.                 local textloc = 5
{txt}  8{com}.                 local party = "Republican"
{txt}  9{com}.         {c )-}
{txt} 10{com}.         if `file'==52204{c -(}
{txt} 11{com}.                 local simlabel = "1964-2016"
{txt} 12{com}.                 local range = 538
{txt} 13{com}.                 local xline = 270
{txt} 14{com}.                 local textloc = 5
{txt} 15{com}.                 local party = "Republican"
{txt} 16{com}.         {c )-}
{txt} 17{com}.         if `file'==52210{c -(}
{txt} 18{com}.                 local simlabel = "1936-1956"
{txt} 19{com}.                 local range = 531
{txt} 20{com}.                 local xline = 266
{txt} 21{com}.                 local textloc = 5
{txt} 22{com}.                 local party = "Republican"
{txt} 23{com}.         {c )-}
{txt} 24{com}.         if `file'==52211{c -(}
{txt} 25{com}.                 local simlabel = "1916-1932"
{txt} 26{com}.                 local range = 531
{txt} 27{com}.                 local xline = 266
{txt} 28{com}.                 local textloc = 5
{txt} 29{com}.                 local party = "Republican"
{txt} 30{com}.         {c )-}
{txt} 31{com}.         if `file'==22202{c -(}
{txt} 32{com}.                 local simlabel = "1872-1888"
{txt} 33{com}.                 local range = 401
{txt} 34{com}.                 local xline = 201
{txt} 35{com}.                 local textloc = 5
{txt} 36{com}.                 local party = "Republican"
{txt} 37{com}.         {c )-}
{txt} 38{com}.         if `file'==12201{c -(}
{txt} 39{com}.                 local simlabel = "1836-1852"
{txt} 40{com}.                 local range = 288
{txt} 41{com}.                 local xline = 145
{txt} 42{com}.                 local textloc = 5
{txt} 43{com}.                 local party = "Whig"
{txt} 44{com}.         {c )-}
{txt} 45{com}.                 
.         //count states 
.         local error=0
{txt} 46{com}.         local counter=1
{txt} 47{com}.         while `error' != 111 {c -(}
{txt} 48{com}.                 qui cap d total_ev_`counter'
{txt} 49{com}.                 local error=_rc
{txt} 50{com}.                 local counter=`counter'+1
{txt} 51{com}.         {c )-}
{txt} 52{com}.         global STATECOUNT = `counter'-2 
{txt} 53{com}.         di "$STATECOUNT"
{txt} 54{com}.         local state_count = `counter'-2 
{txt} 55{com}. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 56{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 57{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 58{com}.         {c )-}
{txt} 59{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 60{com}.         qui gen pvshare = rvotes/pvotes
{txt} 61{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 62{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 63{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 64{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 65{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 66{com}.         {c )-}
{txt} 67{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 68{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 69{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 70{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 71{com}.         qui gen cf_revn_total  = revn_total
{txt} 72{com}.         qui gen cf_rwin = rwin
{txt} 73{com}. 
.         qui gen inv=0
{txt} 74{com}.         qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)
{txt} 75{com}. 
.         
.         local labsize = "medium"
{txt} 76{com}.         hist revn_total  , ///
>                 xtitle("`party' EC Votes", size(`labsize'))  ///
>                 bfcolor(gs8) blcolor(gs8) ///   
>                 graphregion(color(white)) plotregion(color(white)) ///
>                 xsc( r(0 `range')) ///
>                 start(0) fysize(25) width(5) barwidth(3.5) ///
>                 xlabel(0 `xline' `range', nogrid labsize(`labsize')) ///
>                 ylabel(none) xline(`xline', lcolor(gs11) lpattern(dash) ) ///           
>                 ytitle("") ///
>                 text(.0075 `textloc' "`simlabel'" , color(gs8) place(e) justification(left) size(`labsize')) ///
>                 name(hist_`file', replace) saving(figures`c(dirsep)'hist_`file', replace)       
{txt} 77{com}. 
.                 *graph export "figures`c(dirsep)'Main_modern.pdf", replace      
.         
. ************************************************
. *Outer loop
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt} 78{com}. di "$LOOP"
{txt} 79{com}.         
. {c )-}
51
{txt}(bin={res}106{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(file figures/hist_52201.gph saved)
3
51
(bin={res}107{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_52204.gph not found)
{res}{txt}(file figures/hist_52204.gph saved)
4
48
(bin={res}105{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_52210.gph not found)
{res}{txt}(file figures/hist_52210.gph saved)
5
48
(bin={res}103{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_52211.gph not found)
{res}{txt}(file figures/hist_52211.gph saved)
6
38
(bin={res}81{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_22202.gph not found)
{res}{txt}(file figures/hist_22202.gph saved)
7
30
(bin={res}57{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_12201.gph not found)
{res}{txt}(file figures/hist_12201.gph saved)
8

{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. ************************************************
. *Figures
. ************************************************
. 
. 
. graph combine "figures`c(dirsep)'hist_52201.gph" "figures`c(dirsep)'hist_52204.gph" "figures`c(dirsep)'hist_52210.gph" "figures`c(dirsep)'hist_52211.gph" "figures`c(dirsep)'hist_22202.gph" "figures`c(dirsep)'hist_12201.gph", ///
>                 graphregion(color(white)) rows(2) cols(3) xsize(5) ysize(3) xcommon ycommon name(combine2, replace)
{res}{txt}
{com}.                 
. graph export "figures`c(dirsep)'FigureA3b.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA3b.pdf written in PDF format)

{com}. 
. 
{txt}end of do-file

{com}. 
. *Figure A4
. do do_files`c(dirsep)'FigureA4.do
{txt}
{com}. 
.         
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. *NPV Distributions
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. 
. 
. global LIST = "69001 69101 69006 69106 69201" //  Economist Silver correctedEconomist correctedSilver GSE2010
{txt}
{com}. 
. 
. global SD = 100000
{txt}
{com}. global TURNOUT = 150000000
{txt}
{com}. 
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. global STEP = .002      //step along horizontal axis for density plot
{txt}
{com}. global STEP = .004      //step along horizontal axis for density plot
{txt}
{com}. global BINSTART= .42                                    //rightmost point of lowest bin
{txt}
{com}. global BINEND = 1 - $BINSTART + $STEP   //rightmost point of last bin
{txt}
{com}. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define HIST              =J(200, 200, .)         //table for figure: hist
{txt}
{com}. 
. //define bin starts as first row of F
. local row=4
{txt}
{com}. forvalues i=$BINSTART($STEP)$BINEND{c -(} //to hold histogram heights
{txt}  2{com}.         matrix HIST[`row', 1] = `i'
{txt}  3{com}.         local row = `row' + 1
{txt}  4{com}. {c )-}
{txt}
{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 15{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 16{com}.         {c )-}
{txt} 17{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 18{com}.         qui gen pvshare = rvotes/pvotes
{txt} 19{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 20{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 21{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 22{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 23{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 24{com}.         {c )-}
{txt} 25{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 26{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 27{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 28{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 29{com}.         qui gen cf_revn_total  = revn_total
{txt} 30{com}.         qui gen cf_rwin = rwin
{txt} 31{com}. 
.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 32{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 33{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 34{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 35{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim
{txt} 36{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim
{txt} 37{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 38{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 39{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 40{com}.                 label var vmargin_`i' "The margin in vote counts in state `i'"
{txt} 41{com}.                 foreach marg in $MARGLIST{c -(}
{txt} 42{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 43{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 44{com}.                 {c )-}
{txt} 45{com}.         {c )-}
{txt} 46{com}.         
.         gen closest_flippable_state=.
{txt} 47{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 48{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 49{com}.         qui gen tipping_state = .
{txt} 50{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 51{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 52{com}.         {c )-}
{txt} 53{com}. 
.         //EC Reversals
.         egen swingers = rowtotal(swing_*)
{txt} 54{com}.         foreach marg in $MARGLIST{c -(}
{txt} 55{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 56{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 57{com}.                 qui gen notReversed`marg' = swing`marg'==0
{txt} 58{com}.                 label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
{txt} 59{com}.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 60{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 61{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 62{com}.                 
.         {c )-}
{txt} 63{com}.         drop swing`marg'_*
{txt} 64{com}.         
.         //NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 65{com}.                 qui gen PVswing`marg'=0
{txt} 66{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt} 67{com}.                 
.         {c )-}       
{txt} 68{com}. 
.         
.         qui replace swingers = 0 if swingers == .
{txt} 69{com}.         qui gen ec_margin = min_vmargin
{txt} 70{com}.         qui gen pv_margin = margin_votes
{txt} 71{com}.         
.         qui gen zero = swingers == 0
{txt} 72{com}.         label var swingers "number of states that could swing election"
{txt} 73{com}.         
.         *hist swingers, ///
>         *       name(swingers, replace) 
. 
.                 
.         //more state level variables
.         forvalues s=1(1)$STATECOUNT{c -(}
{txt} 74{com}.                 //rename whig variables, as needed
.                 cap rename  W_pvShare_`s'  R_pvShare_`s'
{txt} 75{com}.                 //state vote counts for Rs
.                 qui gen r_votes_`s' = R_pvShare_`s'*twoPartyVotes_`s'
{txt} 76{com}.                 //state evs for Rs
.                 qui gen r_ev_`s' = total_ev_`s' if R_pvShare_`s'>=0.50
{txt} 77{com}.                 qui replace r_ev_`s' = 0 if R_pvShare_`s'<0.50
{txt} 78{com}.         
.         {c )-}
{txt} 79{com}.         
.         //buildup string for row totals
.         local votestring = "r_votes_1"
{txt} 80{com}.         local evstring = "r_ev_1"
{txt} 81{com}.         local totalvotesstring = "twoPartyVotes_1"
{txt} 82{com}.         local totalevstring = "total_ev_1"
{txt} 83{com}.         forvalues s=2(1)$STATECOUNT{c -(} 
{txt} 84{com}.                 local votestring = "`votestring' r_votes_`s'"   // R votes row total string
{txt} 85{com}.                 local evstring = "`evstring' r_ev_`s'"                  // R ev row total string
{txt} 86{com}.                 local totalvotesstring = "`totalvotesstring' twoPartyVotes_`s'" //all votes
{txt} 87{com}.                 local totalevstring = "`totalevstring' total_ev_`s'" //all evs
{txt} 88{com}.         {c )-}
{txt} 89{com}.         
.         //sum to national outcomes
.         qui egen r_votes_natl = rowtotal(`votestring')
{txt} 90{com}.         qui egen r_ev_natl = rowtotal(`evstring')
{txt} 91{com}.         qui egen totalvotes_natl = rowtotal(`totalvotesstring')
{txt} 92{com}.         qui egen totalev_natl = rowtotal(`totalevstring')
{txt} 93{com}.         
.         qui gen cutoff = floor(totalev_natl/2) + 1 
{txt} 94{com}.         qui gen r_share_natl = r_votes_natl/totalvotes_natl
{txt} 95{com}.         qui gen r_ev_win = (r_ev_natl >= cutoff)
{txt} 96{com}.         qui gen r_pv_win = (r_votes_natl>=(totalvotes_natl/2))
{txt} 97{com}.         qui gen inv = r_ev_win!=r_pv_win 
{txt} 98{com}.         label var r_share_natl "R share nationally"
{txt} 99{com}.         label var r_ev_win "R ev win nationally"
{txt}100{com}.         label var r_pv_win "R pv win nationally"
{txt}101{com}.         label var inv "inversion"
{txt}102{com}. 
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt}103{com}.         di `col'
{txt}104{com}.         di `file'
{txt}105{com}.         matrix REC[1, `col']            = `file'
{txt}106{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt}107{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt}108{com}.         matrix RPV[1, `col']            = `file'
{txt}109{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}110{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}111{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}112{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}113{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}114{com}.                 matrix RPV[`row',1] = `marg'
{txt}115{com}.                 
.                 qui summ Reversed`marg'
{txt}116{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}117{com}.                 qui summ Reversed`marg' if dwin==1
{txt}118{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}119{com}.                 qui summ Reversed`marg' if rwin==1
{txt}120{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}121{com}.                 
.                 qui summ PVswing`marg'
{txt}122{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}123{com}.                 
.                 local row = `row'+1
{txt}124{com}.         {c )-}
{txt}125{com}.         
.         
.         matrix list REC
{txt}126{com}.         
.         ************************************************
.         *Data for density figures
.         ************************************************
.         local col = $LOOP
{txt}127{com}.         
.         matrix HIST[1,`col']    = `file'
{txt}128{com}. 
.         local row = 4
{txt}129{com}.         forvalues i= $BINSTART($STEP)$BINEND {c -(}
{txt}130{com}.                 local last = `i'-$STEP
{txt}131{com}.                 local next = `i'
{txt}132{com}.                 matrix HIST[`row', 1] = `i'- $STEP/2    // first column for horizontal axis
{txt}133{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' 
{txt}134{com}.                 local den = r(N)
{txt}135{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' & r_ev_win==1 
{txt}136{com}.                 local num = r(N)        
{txt}137{com}.                 local win = `num'/`den'
{txt}138{com}.                 matrix HIST[`row',`col'] = `den'
{txt}139{com}.                 local row = `row' + 1
{txt}140{com}.         {c )-}
{txt}141{com}. 
. ************************************************
. *Outer loop - close datasets/convert matrices
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}142{com}. di "$LOOP"
{txt}143{com}.         
. {c )-}
51
{txt}(40,000 missing values generated)
2
69001
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    69001        .        .        .        .        .        .        .
{txt} r2 {res}      50  .000575        .        .        .        .        .        .        .
{txt} r3 {res}      75  .000575        .        .        .        .        .        .        .
{txt} r4 {res}     100  .000575        .        .        .        .        .        .        .
{txt} r5 {res}     150    .0006        .        .        .        .        .        .        .
{txt} r6 {res}     225    .0006        .        .        .        .        .        .        .
{txt} r7 {res}     330  .000675        .        .        .        .        .        .        .
{txt} r8 {res}     500  .000725        .        .        .        .        .        .        .
{txt} r9 {res}     750     .001        .        .        .        .        .        .        .
{txt}r10 {res}    1000   .00135        .        .        .        .        .        .        .
{txt}r11 {res}    1500    .0017        .        .        .        .        .        .        .
{txt}r12 {res}    2250    .0024        .        .        .        .        .        .        .
{txt}r13 {res}    3300  .003425        .        .        .        .        .        .        .
{txt}r14 {res}    5000   .00555        .        .        .        .        .        .        .
{txt}r15 {res}    7500  .008075        .        .        .        .        .        .        .
{txt}r16 {res}   10000  .010275        .        .        .        .        .        .        .
{txt}r17 {res}   15000    .0142        .        .        .        .        .        .        .
{txt}r18 {res}   22500  .020125        .        .        .        .        .        .        .
{txt}r19 {res}   33000     .026        .        .        .        .        .        .        .
{txt}r20 {res}   50000    .0341        .        .        .        .        .        .        .
{txt}r21 {res}   75000   .04145        .        .        .        .        .        .        .
{txt}r22 {res}  100000  .046825        .        .        .        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}3
51
(40,000 missing values generated)
3
69101
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    69001    69101        .        .        .        .        .        .
{txt} r2 {res}      50  .000575    .0001        .        .        .        .        .        .
{txt} r3 {res}      75  .000575   .00025        .        .        .        .        .        .
{txt} r4 {res}     100  .000575  .000325        .        .        .        .        .        .
{txt} r5 {res}     150    .0006   .00045        .        .        .        .        .        .
{txt} r6 {res}     225    .0006    .0005        .        .        .        .        .        .
{txt} r7 {res}     330  .000675    .0007        .        .        .        .        .        .
{txt} r8 {res}     500  .000725  .001125        .        .        .        .        .        .
{txt} r9 {res}     750     .001  .001675        .        .        .        .        .        .
{txt}r10 {res}    1000   .00135  .002275        .        .        .        .        .        .
{txt}r11 {res}    1500    .0017    .0034        .        .        .        .        .        .
{txt}r12 {res}    2250    .0024   .00485        .        .        .        .        .        .
{txt}r13 {res}    3300  .003425    .0067        .        .        .        .        .        .
{txt}r14 {res}    5000   .00555    .0108        .        .        .        .        .        .
{txt}r15 {res}    7500  .008075    .0153        .        .        .        .        .        .
{txt}r16 {res}   10000  .010275   .02015        .        .        .        .        .        .
{txt}r17 {res}   15000    .0142   .02855        .        .        .        .        .        .
{txt}r18 {res}   22500  .020125  .037825        .        .        .        .        .        .
{txt}r19 {res}   33000     .026    .0497        .        .        .        .        .        .
{txt}r20 {res}   50000    .0341  .064725        .        .        .        .        .        .
{txt}r21 {res}   75000   .04145   .07915        .        .        .        .        .        .
{txt}r22 {res}  100000  .046825    .0903        .        .        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}4
51
(40,000 missing values generated)
4
69006
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    69001    69101    69006        .        .        .        .        .
{txt} r2 {res}      50  .000575    .0001   .00175        .        .        .        .        .
{txt} r3 {res}      75  .000575   .00025   .00175        .        .        .        .        .
{txt} r4 {res}     100  .000575  .000325   .00175        .        .        .        .        .
{txt} r5 {res}     150    .0006   .00045  .001775        .        .        .        .        .
{txt} r6 {res}     225    .0006    .0005    .0019        .        .        .        .        .
{txt} r7 {res}     330  .000675    .0007  .002125        .        .        .        .        .
{txt} r8 {res}     500  .000725  .001125    .0024        .        .        .        .        .
{txt} r9 {res}     750     .001  .001675  .003475        .        .        .        .        .
{txt}r10 {res}    1000   .00135  .002275  .004875        .        .        .        .        .
{txt}r11 {res}    1500    .0017    .0034    .0072        .        .        .        .        .
{txt}r12 {res}    2250    .0024   .00485    .0098        .        .        .        .        .
{txt}r13 {res}    3300  .003425    .0067  .013925        .        .        .        .        .
{txt}r14 {res}    5000   .00555    .0108  .021525        .        .        .        .        .
{txt}r15 {res}    7500  .008075    .0153  .030925        .        .        .        .        .
{txt}r16 {res}   10000  .010275   .02015    .0407        .        .        .        .        .
{txt}r17 {res}   15000    .0142   .02855   .05715        .        .        .        .        .
{txt}r18 {res}   22500  .020125  .037825   .07965        .        .        .        .        .
{txt}r19 {res}   33000     .026    .0497  .104475        .        .        .        .        .
{txt}r20 {res}   50000    .0341  .064725   .13705        .        .        .        .        .
{txt}r21 {res}   75000   .04145   .07915    .1684        .        .        .        .        .
{txt}r22 {res}  100000  .046825    .0903  .189875        .        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}5
51
(40,000 missing values generated)
5
69106
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    69001    69101    69006    69106        .        .        .        .
{txt} r2 {res}      50  .000575    .0001   .00175  .000725        .        .        .        .
{txt} r3 {res}      75  .000575   .00025   .00175  .000725        .        .        .        .
{txt} r4 {res}     100  .000575  .000325   .00175  .000725        .        .        .        .
{txt} r5 {res}     150    .0006   .00045  .001775   .00095        .        .        .        .
{txt} r6 {res}     225    .0006    .0005    .0019  .000975        .        .        .        .
{txt} r7 {res}     330  .000675    .0007  .002125  .001675        .        .        .        .
{txt} r8 {res}     500  .000725  .001125    .0024    .0024        .        .        .        .
{txt} r9 {res}     750     .001  .001675  .003475  .003875        .        .        .        .
{txt}r10 {res}    1000   .00135  .002275  .004875  .004975        .        .        .        .
{txt}r11 {res}    1500    .0017    .0034    .0072  .007975        .        .        .        .
{txt}r12 {res}    2250    .0024   .00485    .0098  .011475        .        .        .        .
{txt}r13 {res}    3300  .003425    .0067  .013925   .01625        .        .        .        .
{txt}r14 {res}    5000   .00555    .0108  .021525  .024225        .        .        .        .
{txt}r15 {res}    7500  .008075    .0153  .030925   .03365        .        .        .        .
{txt}r16 {res}   10000  .010275   .02015    .0407    .0437        .        .        .        .
{txt}r17 {res}   15000    .0142   .02855   .05715   .06215        .        .        .        .
{txt}r18 {res}   22500  .020125  .037825   .07965  .084325        .        .        .        .
{txt}r19 {res}   33000     .026    .0497  .104475  .110125        .        .        .        .
{txt}r20 {res}   50000    .0341  .064725   .13705  .142575        .        .        .        .
{txt}r21 {res}   75000   .04145   .07915    .1684  .174475        .        .        .        .
{txt}r22 {res}  100000  .046825    .0903  .189875  .199475        .        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}6
51
(10,000 missing values generated)
6
69201
{res}
{txt}REC[30,10]
          c1       c2       c3       c4       c5       c6       c7       c8       c9
 r1 {res}       .    69001    69101    69006    69106    69201        .        .        .
{txt} r2 {res}      50  .000575    .0001   .00175  .000725    .0014        .        .        .
{txt} r3 {res}      75  .000575   .00025   .00175  .000725    .0014        .        .        .
{txt} r4 {res}     100  .000575  .000325   .00175  .000725    .0014        .        .        .
{txt} r5 {res}     150    .0006   .00045  .001775   .00095    .0014        .        .        .
{txt} r6 {res}     225    .0006    .0005    .0019  .000975    .0014        .        .        .
{txt} r7 {res}     330  .000675    .0007  .002125  .001675    .0014        .        .        .
{txt} r8 {res}     500  .000725  .001125    .0024    .0024    .0014        .        .        .
{txt} r9 {res}     750     .001  .001675  .003475  .003875    .0016        .        .        .
{txt}r10 {res}    1000   .00135  .002275  .004875  .004975    .0023        .        .        .
{txt}r11 {res}    1500    .0017    .0034    .0072  .007975    .0023        .        .        .
{txt}r12 {res}    2250    .0024   .00485    .0098  .011475    .0026        .        .        .
{txt}r13 {res}    3300  .003425    .0067  .013925   .01625    .0044        .        .        .
{txt}r14 {res}    5000   .00555    .0108  .021525  .024225    .0071        .        .        .
{txt}r15 {res}    7500  .008075    .0153  .030925   .03365    .0108        .        .        .
{txt}r16 {res}   10000  .010275   .02015    .0407    .0437    .0134        .        .        .
{txt}r17 {res}   15000    .0142   .02855   .05715   .06215    .0182        .        .        .
{txt}r18 {res}   22500  .020125  .037825   .07965  .084325    .0236        .        .        .
{txt}r19 {res}   33000     .026    .0497  .104475  .110125    .0303        .        .        .
{txt}r20 {res}   50000    .0341  .064725   .13705  .142575    .0377        .        .        .
{txt}r21 {res}   75000   .04145   .07915    .1684  .174475    .0447        .        .        .
{txt}r22 {res}  100000  .046825    .0903  .189875  .199475    .0488        .        .        .
{txt}r23 {res}       .        .        .        .        .        .        .        .        .
{txt}r24 {res}       .        .        .        .        .        .        .        .        .
{txt}r25 {res}       .        .        .        .        .        .        .        .        .
{txt}r26 {res}       .        .        .        .        .        .        .        .        .
{txt}r27 {res}       .        .        .        .        .        .        .        .        .
{txt}r28 {res}       .        .        .        .        .        .        .        .        .
{txt}r29 {res}       .        .        .        .        .        .        .        .        .
{txt}r30 {res}       .        .        .        .        .        .        .        .        .

{txt}         c10
 r1 {res}       .
{txt} r2 {res}       .
{txt} r3 {res}       .
{txt} r4 {res}       .
{txt} r5 {res}       .
{txt} r6 {res}       .
{txt} r7 {res}       .
{txt} r8 {res}       .
{txt} r9 {res}       .
{txt}r10 {res}       .
{txt}r11 {res}       .
{txt}r12 {res}       .
{txt}r13 {res}       .
{txt}r14 {res}       .
{txt}r15 {res}       .
{txt}r16 {res}       .
{txt}r17 {res}       .
{txt}r18 {res}       .
{txt}r19 {res}       .
{txt}r20 {res}       .
{txt}r21 {res}       .
{txt}r22 {res}       .
{txt}r23 {res}       .
{txt}r24 {res}       .
{txt}r25 {res}       .
{txt}r26 {res}       .
{txt}r27 {res}       .
{txt}r28 {res}       .
{txt}r29 {res}       .
{txt}r30 {res}       .
{reset}7

{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. cap drop REC*
{txt}
{com}. svmat REC
{txt}
{com}. cap drop RPV*
{txt}
{com}. svmat RPV
{txt}
{com}. //cap drop NPV*
. //svmat NPV
. cap drop HIST*
{txt}
{com}. svmat HIST
{txt}
{com}. 
. //align the bar heights of histograms, given the 10k v 40k sim runs
. replace HIST6=HIST6*4
{txt}(32 real changes made)

{com}. 
. ************************************************
. *Plot
. ************************************************
. 
. 
. 
. 
. local labsize = "small"
{txt}
{com}. twoway ///
>         (connected HIST2 HIST1 in 2/100 ,  lw(thick)  lpattern(..-) lcolor(black) ms(none) ) ///
>         (connected HIST3 HIST1 in 2/100 , lw(thin) lpattern(--.) lcolor(black) ms(none) ) ///
>         (connected HIST4 HIST1 in 2/100 , lw(medium)  lpattern(longdash) lcolor(black) ms(none) ) ///
>         (connected HIST5 HIST1 in 2/100 , lw(medium) lpattern(shortdash) lcolor(black) ms(none) ) ///
>         (connected HIST6 HIST1 in 2/100 , lw(medium) lpattern(dash) lcolor(black) ms(none) ) ///
>         , ///
>         aspect(.5) ///
>         ylabel(0 " ", nogrid labsize(medium) axis(1)) ///
>         xlabel(.42(.02).58, nogrid labsize(`labsize')) ///
>         xline(.5, lcolor(gs11) lpattern(dash) ) ///
>         xtitle("Republican Share of NPV", size(`labsize')) ///
>         ytitle("Probability", size(`labsize') axis(1)) ///
>         graphr(c(white) lc(white)) ///
>         legend(order(1 "Gelman et al., 2020 (uncorrected)" 3 "Gelman et al., 2020 (corrected)"  2 "Silver, 2020 (uncorrected)" 4 "Silver, 2020 (corrected)" 5 "Gelman, Silver, Edlin, 2012") position(2) col(1) ring(0) region(lwidth(thin)) size(small))  ///
>         name(histoverlay_1, replace) 
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'FigureA4a.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA4a.pdf written in PDF format)

{com}.         
. 
.         
.         
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. *ECV Distributions
. ************************************************************************************************************************************************
. ************************************************************************************************************************************************
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"
{txt}
{com}. 
. global LIST = "69001 69101 69006 69106 69201" //  Economist Silver correctedEconomist correctedSilver GSE2010
{txt}
{com}. 
. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
.         
. ************************************************
. *Make vars
. ************************************************        
. 
.         if `file'==52201{c -(}
{txt}  4{com}.                 local simlabel = "Geruso et al., 2022"
{txt}  5{com}.                 local ycoord = .0185
{txt}  6{com}.         {c )-}
{txt}  7{com}.         if `file'==69001{c -(}
{txt}  8{com}.                 local simlabel = "Gelman et al., 2020 uncorrected"
{txt}  9{com}.                 local ycoord = .015
{txt} 10{com}.         {c )-}
{txt} 11{com}.         if `file'==69101{c -(}
{txt} 12{com}.                 local simlabel = "Silver, 2020 uncorrected"
{txt} 13{com}.                 local ycoord = .015
{txt} 14{com}.         {c )-}
{txt} 15{com}.         if `file'==69006{c -(}
{txt} 16{com}.                 local simlabel = "Gelman et al., 2020 corrected"
{txt} 17{com}.                 local ycoord = .015
{txt} 18{com}.         {c )-}
{txt} 19{com}.         if `file'==69106{c -(}
{txt} 20{com}.                 local simlabel = "Silver, 2020 corrected"
{txt} 21{com}.                 local ycoord = .015
{txt} 22{com}.         {c )-}
{txt} 23{com}.         if `file'==69201{c -(}
{txt} 24{com}.                 local simlabel = "Gelman, Silver, Edlin, 2012"
{txt} 25{com}.                 local ycoord = .05
{txt} 26{com}.         {c )-}
{txt} 27{com}. 
.         
.         //count states 
.         local error=0
{txt} 28{com}.         local counter=1
{txt} 29{com}.         while `error' != 111 {c -(}
{txt} 30{com}.                 qui cap d total_ev_`counter'
{txt} 31{com}.                 local error=_rc
{txt} 32{com}.                 local counter=`counter'+1
{txt} 33{com}.         {c )-}
{txt} 34{com}.         global STATECOUNT = `counter'-2 
{txt} 35{com}.         di "$STATECOUNT"
{txt} 36{com}.         local state_count = `counter'-2 
{txt} 37{com}. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 38{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 39{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 40{com}.         {c )-}
{txt} 41{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 42{com}.         qui gen pvshare = rvotes/pvotes
{txt} 43{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 44{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 45{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 46{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 47{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 48{com}.         {c )-}
{txt} 49{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 50{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 51{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 52{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 53{com}.         qui gen cf_revn_total  = revn_total
{txt} 54{com}.         qui gen cf_rwin = rwin
{txt} 55{com}. 
.         qui gen inv=0
{txt} 56{com}.         qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)
{txt} 57{com}. 
.         
.         local labsize = "large"
{txt} 58{com}.         local textsize = "medium"
{txt} 59{com}.         hist revn_total  , ///
>                 xtitle("Republican EC Votes", size(`labsize')) xlabel(0 100  270 400 538, nogrid labsize(`labsize')) ///
>                 bfcolor(gs8) blcolor(gs8) ///   
>                 graphregion(color(white)) plotregion(color(white)) ///
>                 xsc( r(0 538)) ///
>                 start(0) fysize(25) width(5) barwidth(3.5) ///
>                 ylabel(none) xline(270, lcolor(gs11) lpattern(dash) ) ///               
>                 ytitle("") ///
>                 text(`ycoord' 300 "`simlabel'" , color(gs8) place(e) justification(left) size(`textsize')) ///
>                 name(hist_`file', replace) saving(figures`c(dirsep)'hist_`file', replace)               
{txt} 60{com}.         
. ************************************************
. *Outer loop
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt} 61{com}. di "$LOOP"
{txt} 62{com}.         
. {c )-}
51
{txt}(bin={res}77{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_69001.gph not found)
{res}{txt}(file figures/hist_69001.gph saved)
3
51
(bin={res}95{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_69101.gph not found)
{res}{txt}(file figures/hist_69101.gph saved)
4
51
(bin={res}78{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(file figures/hist_69006.gph saved)
5
51
(bin={res}101{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(file figures/hist_69106.gph saved)
6
51
(bin={res}79{txt}, start={res}0{txt}, width={res}5{txt})
{res}{txt}(note: file figures/hist_69201.gph not found)
{res}{txt}(file figures/hist_69201.gph saved)
7

{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. ************************************************
. *Figures
. ************************************************
. 
. graph combine "figures`c(dirsep)'hist_69001.gph" "figures`c(dirsep)'hist_69006.gph" "figures`c(dirsep)'hist_69101.gph" "figures`c(dirsep)'hist_69106.gph" "figures`c(dirsep)'hist_69201.gph", ///
>                 graphregion(color(white)) rows(3) cols(1) xsize(10) ysize(5) xcommon
{res}{txt}
{com}.                 
. graph export "figures`c(dirsep)'FigureA4b.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA4b.pdf written in PDF format)

{com}. 
. 
{txt}end of do-file

{com}. 
. *Figure A5
. do do_files`c(dirsep)'FigureA5.do
{txt}
{com}. 
. 
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. 
. 
. global LIST = "52201 52204 52210 52211  22202 12201" //M1; M12 (1964-2016); 52210 is 1936-1956; 52211 is 1916-1932;  R1; A1
{txt}
{com}. 
. 
. global SD = 100000
{txt}
{com}. global TURNOUT = 150000000
{txt}
{com}. 
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. global STEP = .002      //step along horizontal axis for density plot
{txt}
{com}. global STEP = .004      //step along horizontal axis for density plot
{txt}
{com}. global BINSTART= .36                                    //rightmost point of lowest bin
{txt}
{com}. global BINEND = 1 - $BINSTART + $STEP   //rightmost point of last bin
{txt}
{com}. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define HIST              =J(200, 200, .)         //table for figure: hist
{txt}
{com}. 
. //define bin starts as first row of F
. local row=4
{txt}
{com}. forvalues i=$BINSTART($STEP)$BINEND{c -(} //to hold histogram heights
{txt}  2{com}.         matrix HIST[`row', 1] = `i'
{txt}  3{com}.         local row = `row' + 1
{txt}  4{com}. {c )-}
{txt}
{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 15{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 16{com}.         {c )-}
{txt} 17{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 18{com}.         qui gen pvshare = rvotes/pvotes
{txt} 19{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 20{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 21{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 22{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 23{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 24{com}.         {c )-}
{txt} 25{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 26{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 27{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 28{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 29{com}.         qui gen cf_revn_total  = revn_total
{txt} 30{com}.         qui gen cf_rwin = rwin
{txt} 31{com}. 
.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 32{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 33{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 34{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 35{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim
{txt} 36{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim
{txt} 37{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 38{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 39{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 40{com}.                 label var vmargin_`i' "The margin in vote counts in state `i'"
{txt} 41{com}.                 foreach marg in $MARGLIST{c -(}
{txt} 42{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 43{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 44{com}.                 {c )-}
{txt} 45{com}.         {c )-}
{txt} 46{com}.         
.         gen closest_flippable_state=.
{txt} 47{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 48{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 49{com}.         qui gen tipping_state = .
{txt} 50{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 51{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 52{com}.         {c )-}
{txt} 53{com}. 
.         //EC Reversals
.         egen swingers = rowtotal(swing_*)
{txt} 54{com}.         foreach marg in $MARGLIST{c -(}
{txt} 55{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 56{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 57{com}.                 qui gen notReversed`marg' = swing`marg'==0
{txt} 58{com}.                 label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
{txt} 59{com}.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 60{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 61{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 62{com}.                 
.         {c )-}
{txt} 63{com}.         drop swing`marg'_*
{txt} 64{com}.         
.         //NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 65{com}.                 qui gen PVswing`marg'=0
{txt} 66{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt} 67{com}.                 
.         {c )-}       
{txt} 68{com}. 
.         
.         qui replace swingers = 0 if swingers == .
{txt} 69{com}.         qui gen ec_margin = min_vmargin
{txt} 70{com}.         qui gen pv_margin = margin_votes
{txt} 71{com}.         
.         qui gen zero = swingers == 0
{txt} 72{com}.         label var swingers "number of states that could swing election"
{txt} 73{com}.         
.         *hist swingers, ///
>         *       name(swingers, replace) 
. 
.                 
.         //more state level variables
.         forvalues s=1(1)$STATECOUNT{c -(}
{txt} 74{com}.                 //rename whig variables, as needed
.                 cap rename  W_pvShare_`s'  R_pvShare_`s'
{txt} 75{com}.                 //state vote counts for Rs
.                 qui gen r_votes_`s' = R_pvShare_`s'*twoPartyVotes_`s'
{txt} 76{com}.                 //state evs for Rs
.                 qui gen r_ev_`s' = total_ev_`s' if R_pvShare_`s'>=0.50
{txt} 77{com}.                 qui replace r_ev_`s' = 0 if R_pvShare_`s'<0.50
{txt} 78{com}.         
.         {c )-}
{txt} 79{com}.         
.         //buildup string for row totals
.         local votestring = "r_votes_1"
{txt} 80{com}.         local evstring = "r_ev_1"
{txt} 81{com}.         local totalvotesstring = "twoPartyVotes_1"
{txt} 82{com}.         local totalevstring = "total_ev_1"
{txt} 83{com}.         forvalues s=2(1)$STATECOUNT{c -(} 
{txt} 84{com}.                 local votestring = "`votestring' r_votes_`s'"   // R votes row total string
{txt} 85{com}.                 local evstring = "`evstring' r_ev_`s'"                  // R ev row total string
{txt} 86{com}.                 local totalvotesstring = "`totalvotesstring' twoPartyVotes_`s'" //all votes
{txt} 87{com}.                 local totalevstring = "`totalevstring' total_ev_`s'" //all evs
{txt} 88{com}.         {c )-}
{txt} 89{com}.         
.         //sum to national outcomes
.         qui egen r_votes_natl = rowtotal(`votestring')
{txt} 90{com}.         qui egen r_ev_natl = rowtotal(`evstring')
{txt} 91{com}.         qui egen totalvotes_natl = rowtotal(`totalvotesstring')
{txt} 92{com}.         qui egen totalev_natl = rowtotal(`totalevstring')
{txt} 93{com}.         
.         qui gen cutoff = floor(totalev_natl/2) + 1 
{txt} 94{com}.         qui gen r_share_natl = r_votes_natl/totalvotes_natl
{txt} 95{com}.         qui gen r_ev_win = (r_ev_natl >= cutoff)
{txt} 96{com}.         qui gen r_pv_win = (r_votes_natl>=(totalvotes_natl/2))
{txt} 97{com}.         qui gen inv = r_ev_win!=r_pv_win 
{txt} 98{com}.         label var r_share_natl "R share nationally"
{txt} 99{com}.         label var r_ev_win "R ev win nationally"
{txt}100{com}.         label var r_pv_win "R pv win nationally"
{txt}101{com}.         label var inv "inversion"
{txt}102{com}. 
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt}103{com}.         di `col'
{txt}104{com}.         di `file'
{txt}105{com}.         matrix REC[1, `col']            = `file'
{txt}106{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt}107{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt}108{com}.         matrix RPV[1, `col']            = `file'
{txt}109{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}110{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}111{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}112{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}113{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}114{com}.                 matrix RPV[`row',1] = `marg'
{txt}115{com}.                 
.                 qui summ Reversed`marg'
{txt}116{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}117{com}.                 qui summ Reversed`marg' if dwin==1
{txt}118{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}119{com}.                 qui summ Reversed`marg' if rwin==1
{txt}120{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}121{com}.                 
.                 qui summ PVswing`marg'
{txt}122{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}123{com}.                 
.                 local row = `row'+1
{txt}124{com}.         {c )-}
{txt}125{com}.         
.         
.         matrix list REC
{txt}126{com}.         
.         ************************************************
.         *Data for density figures
.         ************************************************
.         local col = $LOOP
{txt}127{com}.         
.         matrix HIST[1,`col']    = `file'
{txt}128{com}. 
.         local row = 4
{txt}129{com}.         forvalues i= $BINSTART($STEP)$BINEND {c -(}
{txt}130{com}.                 local last = `i'-$STEP
{txt}131{com}.                 local next = `i'
{txt}132{com}.                 matrix HIST[`row', 1] = `i'- $STEP/2    // first column for horizontal axis
{txt}133{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' 
{txt}134{com}.                 local den = r(N)
{txt}135{com}.                 qui count if r_share_natl>`last' & r_share_natl<=`next' & r_ev_win==1 
{txt}136{com}.                 local num = r(N)        
{txt}137{com}.                 local win = `num'/`den'
{txt}138{com}.                 matrix HIST[`row',`col'] = `den'
{txt}139{com}.                 local row = `row' + 1
{txt}140{com}.         {c )-}
{txt}141{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}142{com}. di "$LOOP"
{txt}143{com}.         
. {c )-}
51
{txt}(100,000 missing values generated)
2
52201
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201       .       .       .       .       .       .       .       .
{txt} r2 {res}     50  .00022       .       .       .       .       .       .       .       .
{txt} r3 {res}     75  .00036       .       .       .       .       .       .       .       .
{txt} r4 {res}    100  .00047       .       .       .       .       .       .       .       .
{txt} r5 {res}    150  .00073       .       .       .       .       .       .       .       .
{txt} r6 {res}    225  .00112       .       .       .       .       .       .       .       .
{txt} r7 {res}    330  .00162       .       .       .       .       .       .       .       .
{txt} r8 {res}    500  .00237       .       .       .       .       .       .       .       .
{txt} r9 {res}    750  .00335       .       .       .       .       .       .       .       .
{txt}r10 {res}   1000  .00466       .       .       .       .       .       .       .       .
{txt}r11 {res}   1500  .00678       .       .       .       .       .       .       .       .
{txt}r12 {res}   2250     .01       .       .       .       .       .       .       .       .
{txt}r13 {res}   3300  .01436       .       .       .       .       .       .       .       .
{txt}r14 {res}   5000  .02153       .       .       .       .       .       .       .       .
{txt}r15 {res}   7500  .03137       .       .       .       .       .       .       .       .
{txt}r16 {res}  10000  .04034       .       .       .       .       .       .       .       .
{txt}r17 {res}  15000  .05617       .       .       .       .       .       .       .       .
{txt}r18 {res}  22500   .0747       .       .       .       .       .       .       .       .
{txt}r19 {res}  33000  .09467       .       .       .       .       .       .       .       .
{txt}r20 {res}  50000  .11716       .       .       .       .       .       .       .       .
{txt}r21 {res}  75000  .14006       .       .       .       .       .       .       .       .
{txt}r22 {res} 100000  .15675       .       .       .       .       .       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}3
51
(100,000 missing values generated)
3
52204
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201   52204       .       .       .       .       .       .       .
{txt} r2 {res}     50  .00022  .00012       .       .       .       .       .       .       .
{txt} r3 {res}     75  .00036  .00023       .       .       .       .       .       .       .
{txt} r4 {res}    100  .00047   .0003       .       .       .       .       .       .       .
{txt} r5 {res}    150  .00073  .00039       .       .       .       .       .       .       .
{txt} r6 {res}    225  .00112  .00055       .       .       .       .       .       .       .
{txt} r7 {res}    330  .00162   .0008       .       .       .       .       .       .       .
{txt} r8 {res}    500  .00237  .00112       .       .       .       .       .       .       .
{txt} r9 {res}    750  .00335  .00186       .       .       .       .       .       .       .
{txt}r10 {res}   1000  .00466  .00253       .       .       .       .       .       .       .
{txt}r11 {res}   1500  .00678  .00363       .       .       .       .       .       .       .
{txt}r12 {res}   2250     .01  .00527       .       .       .       .       .       .       .
{txt}r13 {res}   3300  .01436  .00775       .       .       .       .       .       .       .
{txt}r14 {res}   5000  .02153  .01122       .       .       .       .       .       .       .
{txt}r15 {res}   7500  .03137  .01594       .       .       .       .       .       .       .
{txt}r16 {res}  10000  .04034  .02041       .       .       .       .       .       .       .
{txt}r17 {res}  15000  .05617  .02751       .       .       .       .       .       .       .
{txt}r18 {res}  22500   .0747  .03578       .       .       .       .       .       .       .
{txt}r19 {res}  33000  .09467  .04406       .       .       .       .       .       .       .
{txt}r20 {res}  50000  .11716  .05499       .       .       .       .       .       .       .
{txt}r21 {res}  75000  .14006  .06551       .       .       .       .       .       .       .
{txt}r22 {res} 100000  .15675  .07375       .       .       .       .       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}4
48
(100,000 missing values generated)
4
52210
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201   52204   52210       .       .       .       .       .       .
{txt} r2 {res}     50  .00022  .00012  .00017       .       .       .       .       .       .
{txt} r3 {res}     75  .00036  .00023  .00021       .       .       .       .       .       .
{txt} r4 {res}    100  .00047   .0003  .00036       .       .       .       .       .       .
{txt} r5 {res}    150  .00073  .00039   .0005       .       .       .       .       .       .
{txt} r6 {res}    225  .00112  .00055  .00079       .       .       .       .       .       .
{txt} r7 {res}    330  .00162   .0008  .00111       .       .       .       .       .       .
{txt} r8 {res}    500  .00237  .00112  .00176       .       .       .       .       .       .
{txt} r9 {res}    750  .00335  .00186  .00242       .       .       .       .       .       .
{txt}r10 {res}   1000  .00466  .00253  .00318       .       .       .       .       .       .
{txt}r11 {res}   1500  .00678  .00363  .00453       .       .       .       .       .       .
{txt}r12 {res}   2250     .01  .00527  .00659       .       .       .       .       .       .
{txt}r13 {res}   3300  .01436  .00775  .00929       .       .       .       .       .       .
{txt}r14 {res}   5000  .02153  .01122  .01303       .       .       .       .       .       .
{txt}r15 {res}   7500  .03137  .01594  .01746       .       .       .       .       .       .
{txt}r16 {res}  10000  .04034  .02041  .02102       .       .       .       .       .       .
{txt}r17 {res}  15000  .05617  .02751  .02632       .       .       .       .       .       .
{txt}r18 {res}  22500   .0747  .03578  .03184       .       .       .       .       .       .
{txt}r19 {res}  33000  .09467  .04406  .03821       .       .       .       .       .       .
{txt}r20 {res}  50000  .11716  .05499  .04606       .       .       .       .       .       .
{txt}r21 {res}  75000  .14006  .06551  .05428       .       .       .       .       .       .
{txt}r22 {res} 100000  .15675  .07375  .06059       .       .       .       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}5
48
(100,000 missing values generated)
5
52211
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201   52204   52210   52211       .       .       .       .       .
{txt} r2 {res}     50  .00022  .00012  .00017  .00033       .       .       .       .       .
{txt} r3 {res}     75  .00036  .00023  .00021  .00042       .       .       .       .       .
{txt} r4 {res}    100  .00047   .0003  .00036  .00054       .       .       .       .       .
{txt} r5 {res}    150  .00073  .00039   .0005  .00074       .       .       .       .       .
{txt} r6 {res}    225  .00112  .00055  .00079  .00109       .       .       .       .       .
{txt} r7 {res}    330  .00162   .0008  .00111  .00147       .       .       .       .       .
{txt} r8 {res}    500  .00237  .00112  .00176  .00229       .       .       .       .       .
{txt} r9 {res}    750  .00335  .00186  .00242  .00347       .       .       .       .       .
{txt}r10 {res}   1000  .00466  .00253  .00318  .00454       .       .       .       .       .
{txt}r11 {res}   1500  .00678  .00363  .00453  .00659       .       .       .       .       .
{txt}r12 {res}   2250     .01  .00527  .00659  .00955       .       .       .       .       .
{txt}r13 {res}   3300  .01436  .00775  .00929  .01316       .       .       .       .       .
{txt}r14 {res}   5000  .02153  .01122  .01303  .01768       .       .       .       .       .
{txt}r15 {res}   7500  .03137  .01594  .01746  .02239       .       .       .       .       .
{txt}r16 {res}  10000  .04034  .02041  .02102  .02604       .       .       .       .       .
{txt}r17 {res}  15000  .05617  .02751  .02632  .03179       .       .       .       .       .
{txt}r18 {res}  22500   .0747  .03578  .03184  .03841       .       .       .       .       .
{txt}r19 {res}  33000  .09467  .04406  .03821  .04527       .       .       .       .       .
{txt}r20 {res}  50000  .11716  .05499  .04606   .0542       .       .       .       .       .
{txt}r21 {res}  75000  .14006  .06551  .05428  .06348       .       .       .       .       .
{txt}r22 {res} 100000  .15675  .07375  .06059  .07055       .       .       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}6
38
(100,000 missing values generated)
6
22202
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201   52204   52210   52211   22202       .       .       .       .
{txt} r2 {res}     50  .00022  .00012  .00017  .00033  .00245       .       .       .       .
{txt} r3 {res}     75  .00036  .00023  .00021  .00042  .00353       .       .       .       .
{txt} r4 {res}    100  .00047   .0003  .00036  .00054  .00469       .       .       .       .
{txt} r5 {res}    150  .00073  .00039   .0005  .00074  .00674       .       .       .       .
{txt} r6 {res}    225  .00112  .00055  .00079  .00109  .00996       .       .       .       .
{txt} r7 {res}    330  .00162   .0008  .00111  .00147  .01428       .       .       .       .
{txt} r8 {res}    500  .00237  .00112  .00176  .00229  .02079       .       .       .       .
{txt} r9 {res}    750  .00335  .00186  .00242  .00347  .03002       .       .       .       .
{txt}r10 {res}   1000  .00466  .00253  .00318  .00454  .03803       .       .       .       .
{txt}r11 {res}   1500  .00678  .00363  .00453  .00659  .05194       .       .       .       .
{txt}r12 {res}   2250     .01  .00527  .00659  .00955  .06815       .       .       .       .
{txt}r13 {res}   3300  .01436  .00775  .00929  .01316  .08582       .       .       .       .
{txt}r14 {res}   5000  .02153  .01122  .01303  .01768  .10592       .       .       .       .
{txt}r15 {res}   7500  .03137  .01594  .01746  .02239  .12706       .       .       .       .
{txt}r16 {res}  10000  .04034  .02041  .02102  .02604  .14206       .       .       .       .
{txt}r17 {res}  15000  .05617  .02751  .02632  .03179   .1639       .       .       .       .
{txt}r18 {res}  22500   .0747  .03578  .03184  .03841  .18613       .       .       .       .
{txt}r19 {res}  33000  .09467  .04406  .03821  .04527  .20818       .       .       .       .
{txt}r20 {res}  50000  .11716  .05499  .04606   .0542  .23491       .       .       .       .
{txt}r21 {res}  75000  .14006  .06551  .05428  .06348  .26461       .       .       .       .
{txt}r22 {res} 100000  .15675  .07375  .06059  .07055  .28764       .       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}7
30
(100,000 missing values generated)
7
12201
{res}
{txt}REC[30,10]
         c1      c2      c3      c4      c5      c6      c7      c8      c9     c10
 r1 {res}      .   52201   52204   52210   52211   22202   12201       .       .       .
{txt} r2 {res}     50  .00022  .00012  .00017  .00033  .00245  .00682       .       .       .
{txt} r3 {res}     75  .00036  .00023  .00021  .00042  .00353  .00978       .       .       .
{txt} r4 {res}    100  .00047   .0003  .00036  .00054  .00469  .01291       .       .       .
{txt} r5 {res}    150  .00073  .00039   .0005  .00074  .00674  .01887       .       .       .
{txt} r6 {res}    225  .00112  .00055  .00079  .00109  .00996  .02701       .       .       .
{txt} r7 {res}    330  .00162   .0008  .00111  .00147  .01428  .03737       .       .       .
{txt} r8 {res}    500  .00237  .00112  .00176  .00229  .02079  .05167       .       .       .
{txt} r9 {res}    750  .00335  .00186  .00242  .00347  .03002  .06837       .       .       .
{txt}r10 {res}   1000  .00466  .00253  .00318  .00454  .03803  .08161       .       .       .
{txt}r11 {res}   1500  .00678  .00363  .00453  .00659  .05194  .10107       .       .       .
{txt}r12 {res}   2250     .01  .00527  .00659  .00955  .06815  .12238       .       .       .
{txt}r13 {res}   3300  .01436  .00775  .00929  .01316  .08582   .1433       .       .       .
{txt}r14 {res}   5000  .02153  .01122  .01303  .01768  .10592  .16749       .       .       .
{txt}r15 {res}   7500  .03137  .01594  .01746  .02239  .12706  .19285       .       .       .
{txt}r16 {res}  10000  .04034  .02041  .02102  .02604  .14206  .21276       .       .       .
{txt}r17 {res}  15000  .05617  .02751  .02632  .03179   .1639   .2428       .       .       .
{txt}r18 {res}  22500   .0747  .03578  .03184  .03841  .18613  .27718       .       .       .
{txt}r19 {res}  33000  .09467  .04406  .03821  .04527  .20818  .30981       .       .       .
{txt}r20 {res}  50000  .11716  .05499  .04606   .0542  .23491  .33917       .       .       .
{txt}r21 {res}  75000  .14006  .06551  .05428  .06348  .26461   .3597       .       .       .
{txt}r22 {res} 100000  .15675  .07375  .06059  .07055  .28764  .36701       .       .       .
{txt}r23 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r24 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r25 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r26 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r27 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r28 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r29 {res}      .       .       .       .       .       .       .       .       .       .
{txt}r30 {res}      .       .       .       .       .       .       .       .       .       .
{reset}8

{com}. global ENDLOOP = $LOOP
{txt}
{com}. 
. cap drop REC*
{txt}
{com}. svmat REC
{txt}
{com}. cap drop RPV*
{txt}
{com}. svmat RPV
{txt}
{com}. cap drop HIST*
{txt}
{com}. svmat HIST
{txt}
{com}. 
. local labsize = "medium"
{txt}
{com}. twoway ///
>         (connected HIST2 HIST1 in 2/100 , lw(thin)              lcolor(black) ms(none) ) ///
>         (connected HIST3 HIST1 in 2/100 , lw(medium)    lcolor(black) lpattern(--.) ms(none) ) ///
>         (connected HIST4 HIST1 in 2/100 , lw(medthick)  lcolor(black) lpattern(shortdash)  ms(none) ) ///
>         (connected HIST5 HIST1 in 2/100 , lw(medium)    lcolor(black) lpattern(longdash) ms(none) ) ///
>         (connected HIST6 HIST1 in 2/100 , lw(medthick)          lcolor(black) ms(none) ) ///
>         (connected HIST7 HIST1 in 2/100 , lw(medium)    lcolor(black) lpattern(..-) ms(none) ) ///
>         , ///
>         aspect(.25) ///
>         ylabel(0 " ", nogrid labsize(medium) axis(1)) ///
>         xlabel(.36(.02).64, nogrid labsize(`labsize')) ///
>         xline(.5, lcolor(gs11) lpattern(dash) ) ///
>         xtitle("Republican (or Whig) Share of NPV", size(`labsize')) ///
>         ytitle("", size(medium) axis(1)) ///
>         graphr(c(white) lc(white)) ///
>         legend(order(1 "1988-2016" 2 "1964-2016" 3 "1936-1956" 4 "1916-1932" 5 "1872-1888" 6 "1836-1852") row(2) size(medium)) /// position(5) col(1) ring(0) region(lwidth(thin)) size(small))  ///
>         name(histoverlay_historical, replace) 
{res}{txt}
{com}.         
.         graph export "figures`c(dirsep)'FigureA5.pdf", replace
{txt}(file /Users/mlg2296/Dropbox/recount/ReplicationPacket/figures/FigureA5.pdf written in PDF format)

{com}. 
{txt}end of do-file

{com}. 
. *Table 1
. do do_files`c(dirsep)'Table1.do
{txt}
{com}. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. clear all
{res}{txt}
{com}. set more off
{txt}
{com}. set obs 1000000
{txt}{p}
number of observations (_N)  was 0,
now 1,000,000
{p_end}

{com}. 
. set seed 55201
{txt}
{com}. 
. qui gen a1 = rnormal()*0.02 + 0.45 // KS 42.5 D SC 44.1
{txt}
{com}. qui gen a2 = rnormal()*0.02 + 0.51 // PA 50.6 D
{txt}
{com}. qui gen a3 = rnormal()*0.02 + 0.57 // CO 56.9 D
{txt}
{com}. 
. qui gen b1 = rnormal()*0.02 + 0.37 // SD 36.6
{txt}
{com}. qui gen b2 = rnormal()*0.02 + 0.54 // Minn 53.6 D TX 52.8 R 
{txt}
{com}. qui gen b3 = rnormal()*0.02 + 0.62 // NY 61.7
{txt}
{com}. 
. qui gen c1 = rnormal()*0.02 + 0.42 // 
{txt}
{com}. qui gen c2 = rnormal()*0.02 + 0.54 // Minn 53.6 D TX 52.8 R 
{txt}
{com}. qui gen c3 = rnormal()*0.02 + 0.57 // 
{txt}
{com}.                                                                         
. qui gen d0 = rnormal()*0.0254 + 0.37 // 
{txt}
{com}. qui gen d1 = rnormal()*0.0254 + 0.42 // KS 42.5 D SC 44.1
{txt}
{com}. qui gen d2 = rnormal()*0.0736 + 0.54 // 
{txt}
{com}. qui gen d3 = rnormal()*0.0254 + 0.60 // 
{txt}
{com}. qui gen d4 = rnormal()*0.0254 + 0.62 // 
{txt}
{com}. 
. //NPV outcome
. qui gen a = (a1+a2+a3)/3 //map a average
{txt}
{com}. qui gen b = (b1+b2+b3)/3 //map b average
{txt}
{com}. qui gen c = (d1+c2+c3)/3 //map c average
{txt}
{com}. qui gen d = (d0+d1+d2+d3+d4)/5 //map d average
{txt}
{com}. 
. //EC Margins
. qui gen ec_a2 = abs(a2-.5)*10000 
{txt}
{com}. qui gen ec_b2 = abs(b2-.5)*10000
{txt}
{com}. qui gen ec_c2 = abs(c2-.5)*10000
{txt}
{com}. qui gen ec_d2 = abs(d2-.5)*6000
{txt}
{com}. 
. //NPV margins
. qui gen npv_a = abs(a-.5)*30000
{txt}
{com}. qui gen npv_b = abs(b-.5)*30000
{txt}
{com}. qui gen npv_c = abs(a-.5)*30000
{txt}
{com}. qui gen npv_d = abs(b-.5)*30000
{txt}
{com}. 
. sum a b a2 b2

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 11}a {c |}{res}  1,000,000    .5100094    .0115382   .4515302   .5650432
{txt}{space 11}b {c |}{res}  1,000,000    .5099997    .0115444    .454275   .5643616
{txt}{space 10}a2 {c |}{res}  1,000,000    .5099805    .0199944   .4111577   .6095036
{txt}{space 10}b2 {c |}{res}  1,000,000    .5400112    .0200079   .4425632   .6490639
{txt}
{com}. 
. matrix define T=J(4, 4, .)
{txt}
{com}. forvalues i=1(1)4{c -(}
{txt}  2{com}.         matrix T[`i', 1] = 51
{txt}  3{com}. {c )-}
{txt}
{com}. 
. 
. matrix SHARES=(.,45,51,57,.\.,37,54,62,.\.,42,54,57,.\37,42,54,60,62)
{txt}
{com}. matrix list SHARES
{res}
{txt}SHARES[4,5]
    c1  c2  c3  c4  c5
r1 {res}  .  45  51  57   .
{txt}r2 {res}  .  37  54  62   .
{txt}r3 {res}  .  42  54  57   .
{txt}r4 {res} 37  42  54  60  62
{reset}
{com}. 
. matrix SIZE = (10000\10000\10000\6000)
{txt}
{com}. matrix list SIZE
{res}
{txt}SIZE[4,1]
       c1
r1 {res} 10000
{txt}r2 {res} 10000
{txt}r3 {res} 10000
{txt}r4 {res}  6000
{reset}
{com}. 
. 
. local i=1
{txt}
{com}. foreach var in a b c d {c -(}
{txt}  2{com}.         
.         summ npv_`var'
{txt}  3{com}.         matrix T[`i', 4] = r(mean)
{txt}  4{com}.         summ ec_`var'2
{txt}  5{com}.         matrix T[`i', 3] = r(mean)
{txt}  6{com}.         
.         local i=`i'+1
{txt}  7{com}. {c )-}

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}npv_a {c |}{res}  1,000,000    374.1061    264.6341          0   1951.296

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}ec_a2 {c |}{res}  1,000,000    178.9748    133.8165          0   1095.036

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}npv_b {c |}{res}  1,000,000    374.1049    264.5463   .0008941   1930.847

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}ec_b2 {c |}{res}  1,000,000    403.5014    193.1515   .0029802   1490.639

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}npv_c {c |}{res}  1,000,000    374.1061    264.6341          0   1951.296

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}ec_c2 {c |}{res}  1,000,000    403.0363    192.9788   .0011921   1503.409

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}npv_d {c |}{res}  1,000,000    374.1049    264.5463   .0008941   1930.847

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 7}ec_d2 {c |}{res}  1,000,000    403.2691    299.9448   .0001788    2574.72
{txt}
{com}. 
. matrix list T
{res}
{txt}T[4,4]
           c1         c2         c3         c4
r1 {res}        51          .  178.97483  374.10605
{txt}r2 {res}        51          .  403.50139  374.10487
{txt}r3 {res}        51          .  403.03628  374.10605
{txt}r4 {res}        51          .  403.26914  374.10487
{reset}
{com}. matrix TABLE1=(SIZE,SHARES,T)
{txt}
{com}. 
. 
. matrix list TABLE1
{res}
{txt}TABLE1[4,10]
           c1         c1         c2         c3         c4         c5         c1
r1 {res}     10000          .         45         51         57          .         51
{txt}r2 {res}     10000          .         37         54         62          .         51
{txt}r3 {res}     10000          .         42         54         57          .         51
{txt}r4 {res}      6000         37         42         54         60         62         51

{txt}           c2         c3         c4
r1 {res}         .  178.97483  374.10605
{txt}r2 {res}         .  403.50139  374.10487
{txt}r3 {res}         .  403.03628  374.10605
{txt}r4 {res}         .  403.26914  374.10487
{reset}
{com}. xml_tab TABLE1, save("tables`c(dirsep)'Table1") replace


{txt}note: results saved to tables/Table1.xml

{com}. 
{txt}end of do-file

{com}. 
. *Table A2
. do do_files`c(dirsep)'TableA2.do
{txt}
{com}. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. ************************************************
. *Set list of margins; becomes points on x axis
. ************************************************
. 
. global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
{txt}
{com}. global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"
{txt}
{com}. 
. 
. 
. global LIST = "52201"
{txt}
{com}. 
. 
. ************************************************
. *Storage for Summary Statistics
. ************************************************
. 
. matrix define REC               =J(30, 10, .)           //table of summary of reversal rates;                                           columns are models, rows are critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECPCT    =J(30, 10, .)           //table of summary of reversal rates based on % margins;        columns are models, rows are %critical margins of votes dropped from winner or gained by loser
{txt}
{com}. matrix define RECDECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on D EC WINS
{txt}
{com}. matrix define RECRECWIN =J(30, 10, .)           //table of summary of reversal rates; condl on R EC WINS
{txt}
{com}. matrix define RECRECFRACWIN     =J(30, 10, .)           //table of summary of reversal rates; condl frac of R EC WINS
{txt}
{com}. matrix define RPV               =J(30, 10, .)           //table of summary of NPV reversal rates;
{txt}
{com}. matrix define RPVPCT    =J(30, 10, .)           //table of summary of NPV reversal rates; in % margins (pp)
{txt}
{com}. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
. 
. global LOOP = 2
{txt}
{com}. 
. foreach file in $LIST{c -(}
{txt}  2{com}. 
.         use "source_data`c(dirsep)'`file'.dta", clear
{txt}  3{com}. 
. ************************************************
. *stats and vars
. ************************************************
. 
. //count states 
. local error=0
{txt}  4{com}. local counter=1
{txt}  5{com}. while `error' != 111 {c -(}
{txt}  6{com}.         qui cap d total_ev_`counter'
{txt}  7{com}.         local error=_rc
{txt}  8{com}.         local counter=`counter'+1
{txt}  9{com}. {c )-}
{txt} 10{com}. global STATECOUNT = `counter'-2 
{txt} 11{com}. di "$STATECOUNT"
{txt} 12{com}. local state_count = `counter'-2 
{txt} 13{com}. 
. 
.         //make vars that summarize each election
.         qui egen pvotes = rowtotal(twoPartyVotes_*)
{txt} 14{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 15{com}.                 qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
{txt} 16{com}.         {c )-}
{txt} 17{com}.         qui egen rvotes = rowtotal(rvotes_*)
{txt} 18{com}.         qui gen pvshare = rvotes/pvotes
{txt} 19{com}.         qui gen margin_share = abs(0.5-pvshare)*2
{txt} 20{com}.         qui gen margin_votes = margin_share*pvotes
{txt} 21{com}. 
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 22{com}.                 qui gen rwin_`i' = R_pvShare_`i' > .5
{txt} 23{com}.                 qui gen rev_`i' = rwin_`i' * total_ev_`i'
{txt} 24{com}.         {c )-}
{txt} 25{com}.         qui egen revn_total= rowtotal(rev_*)
{txt} 26{com}.         qui egen ev_denominator = rowtotal(total_ev_*)
{txt} 27{com}.         qui gen rwin = revn_total/ev_denominator > 0.5
{txt} 28{com}.         qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
{txt} 29{com}.         qui gen cf_revn_total  = revn_total
{txt} 30{com}.         qui gen cf_rwin = rwin
{txt} 31{com}. 
.         qui gen inv=0
{txt} 32{com}.         qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)
{txt} 33{com}.         
.         //This loop identifies which states can flip election on their own (swing==1) and if so, 
.         //how many votes must be dropped to do it(vmargin==X)
.         forvalues i = 1(1)`state_count'{c -(}
{txt} 34{com}.                 qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
{txt} 35{com}.                 qui replace cf_revn_total = revn_total //reset each loop
{txt} 36{com}.                 qui replace cf_rwin = rwin //reset each loop
{txt} 37{com}.                 qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim (nationally)
{txt} 38{com}.                 qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim (nationally)
{txt} 39{com}.                 qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
{txt} 40{com}.                 qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
{txt} 41{com}.                 //^this allows a change from not a tie to a tie or vice versa to count as a change
.                 qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
{txt} 42{com}.                 label var vmargin_`i' "The margin in vote counts in state `i', if `i' could flip EC"
{txt} 43{com}.                 
.                 //Levels: cylce through marglist to get probability of flips at each margin
.                 foreach marg in $MARGLIST{c -(}
{txt} 44{com}.                         qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
{txt} 45{com}.                         label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
{txt} 46{com}.                 {c )-}
{txt} 47{com}.                 //PP: cylce through % marglist to get probability of flips at each margin
.                 foreach marg in $MARGLISTPCT{c -(}
{txt} 48{com}.                         local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 49{com}.                         qui gen swingpct`labelmarg'_`i' = swing_`i'==1 & (vmargin_`i'/pvote)<`marg'
{txt} 50{com}.                         label var swingpct`labelmarg'_`i' "Binary: is vmargin_`i' less than `marg' expressed as pct of turnout"
{txt} 51{com}.                 {c )-}       
{txt} 52{com}.                 
.         {c )-}
{txt} 53{com}.         
.         qui gen closest_flippable_state=.
{txt} 54{com}.         qui egen min_vmargin = rowmin(vmargin_*) 
{txt} 55{com}.         label var min_vmargin "minimum votes needed to flip closest state"
{txt} 56{com}.         qui gen tipping_state = .
{txt} 57{com}.         forvalues i = 1(1)`state_count'{c -(}
{txt} 58{com}.                 qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
{txt} 59{com}.         {c )-}
{txt} 60{com}.         egen swingers = rowtotal(swing_*)
{txt} 61{com}.         
.         //Levels EC Reversals 
.         foreach marg in $MARGLIST{c -(}
{txt} 62{com}.                 qui egen swing`marg' = rowtotal(swing`marg'_*)
{txt} 63{com}.                 qui replace swing`marg' = 0 if swing`marg' == .
{txt} 64{com}.                 *qui gen notReversed`marg' = swing`marg'==0
.                 *label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
.                 qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
{txt} 65{com}.                 qui replace Reversed`marg' = 0 if swing`marg' == .
{txt} 66{com}.                 label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
{txt} 67{com}.         {c )-}
{txt} 68{com}.                 
.         //PP EC Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt} 69{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 70{com}.                 di `labelmarg'
{txt} 71{com}.                 qui egen swingpct`labelmarg' = rowtotal(swingpct`labelmarg'_*)
{txt} 72{com}.                 qui replace swingpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 73{com}.                 *qui gen notReversed`marg' = swing`marg'==0
.                 *label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
.                 qui gen Reversedpct`labelmarg' = (swingpct`labelmarg'>0 & swingpct`labelmarg'<51)
{txt} 74{com}.                 qui replace Reversedpct`labelmarg' = 0 if swingpct`labelmarg' == .
{txt} 75{com}.                 label var Reversedpct`labelmarg' "EC reversed by at least one state dropping `marg' pp votes"
{txt} 76{com}.         {c )-}
{txt} 77{com}.         
.         //Levels NPV Reversals
.         foreach marg in $MARGLIST{c -(}
{txt} 78{com}.                 qui gen PVswing`marg'=0
{txt} 79{com}.                 qui replace PVswing`marg' = 1 if margin_votes<`marg'
{txt} 80{com}.         {c )-}
{txt} 81{com}.         //PP NPV Reversals
.         foreach marg in $MARGLISTPCT{c -(}
{txt} 82{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt} 83{com}.                 qui gen PVswingpct`labelmarg'=0
{txt} 84{com}.                 qui replace PVswingpct`labelmarg' = 1 if (margin_votes/pvotes)<`marg'
{txt} 85{com}.         {c )-}       
{txt} 86{com}. 
.         qui replace swingers = 0 if swingers == .
{txt} 87{com}.         qui gen ec_margin = min_vmargin
{txt} 88{com}.         qui gen pv_margin = margin_votes
{txt} 89{com}.         
.         qui gen zero = swingers == 0
{txt} 90{com}.         label var swingers "number of states that could swing election"
{txt} 91{com}.                 
.         ************************************************
.         *Data for  figures
.         ************************************************
.         local col = $LOOP
{txt} 92{com}.         di `col'
{txt} 93{com}.         di `file'
{txt} 94{com}.         matrix REC[1, `col']            = `file'
{txt} 95{com}.         matrix RECPCT[1, `col']         = `file'
{txt} 96{com}.         matrix RECDECWIN[1, `col']      = `file'
{txt} 97{com}.         matrix RECRECWIN[1, `col']      = `file'
{txt} 98{com}.         matrix RECRECFRACWIN[1, `col']  = `file'        
{txt} 99{com}.         matrix RPV[1, `col']            = `file'
{txt}100{com}.         matrix RPVPCT[1, `col']         = `file'        
{txt}101{com}. 
.         local row = 2 //place marglist in rows starting at 2
{txt}102{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLIST{c -(}
{txt}103{com}. 
.                 matrix REC[`row',1] = `marg'
{txt}104{com}.                 matrix RECDECWIN[`row',1] = `marg'
{txt}105{com}.                 matrix RECRECWIN[`row',1] = `marg'
{txt}106{com}.                 matrix RECRECFRACWIN[`row',1] = `marg'
{txt}107{com}.                 matrix RPV[`row',1] = `marg'
{txt}108{com}.                 
.                 qui summ Reversed`marg'
{txt}109{com}.                 matrix REC[`row',`col'] = r(mean)
{txt}110{com}.                 
.                 qui summ Reversed`marg' if dwin==1
{txt}111{com}.                 matrix RECDECWIN[`row',`col'] = r(mean)
{txt}112{com}.                 qui count if Reversed`marg'==1 & dwin==1        
{txt}113{com}.                 local decwin = r(N)
{txt}114{com}.                 
.                 qui summ Reversed`marg' if rwin==1
{txt}115{com}.                 matrix RECRECWIN[`row',`col'] = r(mean)
{txt}116{com}.                 qui count if Reversed`marg'==1 & rwin==1
{txt}117{com}.                 local recwin = r(N)
{txt}118{com}.                 
.                 matrix RECRECFRACWIN[`row',`col']=`recwin'/(`recwin'+`decwin')
{txt}119{com}.                 
.                 qui summ PVswing`marg'
{txt}120{com}.                 matrix RPV[`row',`col'] = r(mean)               
{txt}121{com}.                 
.                 local row = `row'+1
{txt}122{com}.         {c )-}
{txt}123{com}.         
.         local row = 2 //place marglist in rows starting at 2
{txt}124{com}.         //stores fraction of sims reversed by indicated margin
.         foreach marg in $MARGLISTPCT{c -(}
{txt}125{com}.                 local labelmarg = round(1/`marg')  //cant have decimals in localvars used as names
{txt}126{com}.                 matrix RECPCT[`row',1] = `marg'
{txt}127{com}.                 matrix RPVPCT[`row',1] = `marg'
{txt}128{com}.                 
.                 qui summ Reversedpct`labelmarg'
{txt}129{com}.                 matrix RECPCT[`row',`col'] = r(mean)
{txt}130{com}.                 qui summ PVswingpct`labelmarg'
{txt}131{com}.                 matrix RPVPCT[`row',`col'] = r(mean)            
{txt}132{com}.                 
.                 local row = `row'+1
{txt}133{com}.         {c )-}       
{txt}134{com}.         
.         
.         ************************************************
.         *Match Moments
.         ************************************************
.         //match moments
.         
.         /* Notes
>         
>         z = invnormal(p) returns the z score X that generates the probability p: Pr(z<X)=p, 
>         so invnormal(-.025) returns -1.96
>         
>         
>         Generate statistics to match:
>         
>         //find corresponding sigmas
>         //Int^{c -(}z/2{c )-}_{c -(}z/2{c )-}(Phi) = Int^{c -(}m/sigma*2{c )-}_{c -(}m/sigma*2{c )-}(Phi) = p
>         //test function is single sided, so divide p by 2 and subtract from .5. Example for p
>         //sigma of interest is margin (in pp) divided by the z returned
>         //Z = marg/(2*sigma) (both m and sigma in pp terms), sigma = marg/(2*Z)
>         */
.         
.         
.         
.         //Make vote shares Normal
.         cap drop marginMatch
{txt}135{com}.         cap drop pMatch
{txt}136{com}.         gen marginMatch = .
{txt}137{com}.         
.         gen pMatch = .
{txt}138{com}.         
.         local i = 1
{txt}139{com}.         foreach marg in $MARGLISTPCT{c -(}
{txt}140{com}.                 local labelmarg = round(1/`marg')
{txt}141{com}.         
.                 replace marginMatch = `marg'*100 in `i' //marg in pp (so (votes/turnout) * 100)
{txt}142{com}.                 
.                 summ Reversedpct`labelmarg'
{txt}143{com}.                 replace pMatch = r(mean) in `i'
{txt}144{com}.                 local i = `i' + 1
{txt}145{com}.         {c )-}
{txt}146{com}.         
.         cap drop sigmaMatch
{txt}147{com}.         gen sigmaMatch = -marginMatch/(2*invnormal(0.5 - (pMatch/2)))
{txt}148{com}.         
.         gen marginMatchTurnout = 150000000*marginMatch/100 //margin match are in points
{txt}149{com}.         mkmat marginMatch marginMatchTurnout pMatch sigmaMatch in 1/15, matrix(MATCH)
{txt}150{com}.         
.         xml_tab MATCH, save("tables`c(dirsep)'TableA2") replace
{txt}151{com}.         
. 
. ************************************************
. *Outer loop - open datasets
. ************************************************
.         
. global LOOP = $LOOP +1          
{txt}152{com}. di "$LOOP"
{txt}153{com}.         
. {c )-}
51
10000000
5000000
2000000
1000000
500000
200000
100000
50000
20000
10000
5000
2000
1000
2
52201
{txt}(100,000 missing values generated)
(100,000 missing values generated)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rev~10000000 {c |}{res}    100,000      .00009    .0094865          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~5000000 {c |}{res}    100,000      .00017    .0130374          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~2000000 {c |}{res}    100,000      .00033     .018163          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~1000000 {c |}{res}    100,000      .00066    .0256821          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~500000 {c |}{res}    100,000      .00136    .0368533          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~200000 {c |}{res}    100,000      .00314    .0559479          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reve~t100000 {c |}{res}    100,000      .00628    .0789976          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t50000 {c |}{res}    100,000      .01208    .1092437          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~20000 {c |}{res}    100,000      .02889    .1674982          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Rever~t10000 {c |}{res}    100,000      .05251    .2230542          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t5000 {c |}{res}    100,000      .08439    .2779732          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Reverse~2000 {c |}{res}    100,000      .13472     .341426          0          1
{txt}(1 real change made)
(1 real change made)

    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
Revers~t1000 {c |}{res}    100,000      .17543    .3803364          0          1
{txt}(1 real change made)
(99,987 missing values generated)
(99,987 missing values generated)
{res}

{txt}note: results saved to tables/TableA2.xml
3

{com}. 
{txt}end of do-file

{com}. 
. *Table A3
. do do_files`c(dirsep)'TableA3.do
{txt}
{com}. 
. 
. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. clear all
{res}{txt}
{com}. set more off
{txt}
{com}. set obs 1000000
{txt}{p}
number of observations (_N)  was 0,
now 1,000,000
{p_end}

{com}. set seed 20210707
{txt}
{com}. 
. local a1 = 0.35
{txt}
{com}. local a2 = 0.53
{txt}
{com}. local a3 = 0.65
{txt}
{com}. local b1 = 0.2
{txt}
{com}. local b2 = 0.63
{txt}
{com}. local b3 = 0.7
{txt}
{com}. local sd = 0.202
{txt}
{com}. foreach map in a b{c -(}
{txt}  2{com}. forvalues s = 1(1)3{c -(}
{txt}  3{com}. gen `map'`s' = invlogit(logit(``map'`s'')+ rnormal()*`sd')
{txt}  4{com}. gen w_`map'`s' = `map'`s' > 0.5
{txt}  5{com}. {c )-}
{txt}  6{com}. 
. gen pv_`map' = (`map'1 + `map'2 + `map'3)/3
{txt}  7{com}. gen close_pv_`map' = abs(pv_`map'-0.5)*3000 
{txt}  8{com}. 
. gen normal_`map' = w_`map'1 != w_`map'3
{txt}  9{com}. gen all3_`map' = w_`map'1 == w_`map'3 & w_`map'1 == w_`map'2
{txt} 10{com}. 
. gen close_ev_`map' = abs(`map'2-0.5)*1000 if normal_`map' == 1 
{txt} 11{com}. replace close_ev_`map' = abs(`map'1-0.5)*1000 if normal_`map' != 1&  all3_`map' == 0 & `map'2<`map'1<`map'3
{txt} 12{com}. replace close_ev_`map' = abs(`map'3-0.5)*1000 if normal_`map' != 1&  all3_`map' == 0 & `map'1<`map'3<`map'2
{txt} 13{com}. replace close_ev_`map' = abs(`map'3-0.5)*1000 if `map'1<`map'3& `map'3<`map'2&w_`map'3!=w_`map'2
{txt} 14{com}. replace close_ev_`map' = abs(`map'1-0.5)*1000 if `map'2<`map'1& `map'1<`map'3 & w_`map'1 != w_`map'2
{txt} 15{com}. replace close_ev_`map' = abs(`map'3-0.5)*1000 + abs(`map'2-0.5)*1000 if normal_`map' != 1&  all3_`map' == 1 
{txt} 16{com}. replace close_ev_`map' = abs(`map'1-0.5)*1000 + abs(`map'2-0.5)*1000 if normal_`map' != 1&  all3_`map' == 1  & abs(`map'1-0.5)*1000 + abs(`map'2-0.5)*1000 < close_ev_`map'
{txt} 17{com}. replace close_ev_`map' = abs(`map'1-0.5)*1000 + abs(`map'3-0.5)*1000 if normal_`map' != 1&  all3_`map' == 1  & abs(`map'1-0.5)*1000 + abs(`map'2-0.5)*1000 < close_ev_`map'
{txt} 18{com}. 
. gen assumptions_hold_`map' = `map'1<`map'2& `map'2<`map'3&w_`map'3!=w_`map'1&0 < `map'1&`map'3<1
{txt} 19{com}. {c )-}
{txt}(2,149 missing values generated)
(742 real changes made)
(3 real changes made)
(740 real changes made)
(295 real changes made)
(1,111 real changes made)
(810 real changes made)
(329 real changes made)
(15 missing values generated)
(15 real changes made)
(0 real changes made)
(15 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)

{com}. 
. 
. 
. sum close_*

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 2}close_pv_a {c |}{res}  1,000,000    69.45117    52.15633          0   402.2614
{txt}{space 2}close_ev_a {c |}{res}  1,000,000    46.86001    34.35504   .0000596   289.6633
{txt}{space 2}close_pv_b {c |}{res}  1,000,000    61.50209    45.75756          0   373.7441
{txt}{space 2}close_ev_b {c |}{res}  1,000,000    128.9506    46.37833   .0005364   319.9273
{txt}
{com}. sum a1 a2 a3 b1 b2 b3

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 10}a1 {c |}{res}  1,000,000    .3513873     .045643   .1713523   .6047294
{txt}{space 10}a2 {c |}{res}  1,000,000    .5297795    .0498273   .2984803   .7548946
{txt}{space 10}a3 {c |}{res}  1,000,000    .6486512    .0456922   .4138807   .8330437
{txt}{space 10}b1 {c |}{res}  1,000,000    .2019675    .0324384   .0881555   .4020008
{txt}{space 10}b2 {c |}{res}  1,000,000    .6288157    .0467535   .3953609   .8199273
{txt}{hline 13}{c +}{hline 57}
{space 10}b3 {c |}{res}  1,000,000    .6982745    .0423283   .4703062   .8680022
{txt}
{com}. sum pv_a pv_b

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 8}pv_a {c |}{res}  1,000,000    .5099394     .027192   .3748876   .6340871
{txt}{space 8}pv_b {c |}{res}  1,000,000    .5096859    .0236453   .3754186    .617606
{txt}
{com}. sum assumptions*

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
assumption~a {c |}{res}  1,000,000     .953769    .2099851          0          1
{txt}assumption~b {c |}{res}  1,000,000     .865091    .3416265          0          1
{txt}
{com}. 
. foreach v in a1 a2 a3 b1 b2 b3 pv_a pv_b close_pv_a close_ev_a close_pv_b close_ev_b{c -(}
{txt}  2{com}.         qui sum `v'
{txt}  3{com}.         qui local m`v' = r(mean)
{txt}  4{com}. {c )-}
{txt}
{com}. 
. 
. matrix T=(1000, `ma1', `ma2', `ma3', `mpv_a', `mclose_ev_a', `mclose_pv_a' \ 1000, `mb1', `mb2', `mb3', `mpv_b', `mclose_ev_b', `mclose_pv_b')
{txt}
{com}. matrix list T
{res}
{txt}T[2,7]
           c1         c2         c3         c4         c5         c6         c7
r1 {res}      1000  .35138732  .52977949  .64865125  .50993935  46.860012  69.451172
{txt}r2 {res}      1000  .20196748  .62881569   .6982745  .50968589  128.95056  61.502085
{reset}
{com}. xml_tab T, save("tables`c(dirsep)'TableA3") replace


{txt}note: results saved to tables/TableA3.xml

{com}. 
{txt}end of do-file

{com}. 
. *Table A4 and A5
. do do_files`c(dirsep)'TableA4A5.do
{txt}
{com}. 
. clear
{txt}
{com}. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. /*****************************************************
> Load and Reformat data
> *****************************************************/
. 
. use "source_data`c(dirsep)'state_election_results.dta"
{txt}
{com}. 
. gen Year2 = substr(Year,1,4) //some extra characters in the 5,6th character locations
{txt}
{com}. destring Year2, gen(year) force
{txt}Year2: all characters numeric; year {res}generated {txt}as {res}int
{txt}
{com}. *tab Year2, summ(year)
. 
. /*****************************************************
> Sample Restriction
> *****************************************************/
. keep if year>=1960 & year<=2020
{txt}(564 observations deleted)

{com}. egen group = group(state racetype)
{txt}
{com}. 
. /*****************************************************
> Make Vars
> *****************************************************/
. 
. 
. 
. //Find two party vote share
. gen Rshare = Rep/(Dem + Rep)
{txt}
{com}. 
. //Flag and drop if independent or third party win
. gen absIshare = Ind/(Dem + Rep + Ind + Other)
{txt}
{com}. gen absOshare = Other/(Dem + Rep + Ind + Other)
{txt}
{com}. gen absDshare = Dem/(Dem + Rep + Ind + Other)
{txt}
{com}. gen absRshare = Rep/(Dem + Rep + Ind + Other)
{txt}
{com}. 
. gen IOwinFlag = 0
{txt}
{com}. replace IOwinFlag =1 if absIshare>absDshare  & absIshare>absRshare
{txt}(15 real changes made)

{com}. replace IOwinFlag =1 if absOshare>absDshare  & absOshare>absRshare
{txt}(3 real changes made)

{com}. 
. drop if IOwinFlag==1
{txt}(18 observations deleted)

{com}. 
. sort group year
{txt}
{com}. 
. egen RShareSD = sd(Rshare), by(group)
{txt}
{com}. egen RShareMean = mean(Rshare), by(group)
{txt}
{com}. 
. 
. gen obs = 1
{txt}
{com}. 
. collapse (first) RShareSD RShareMean (count) obs, by(state racetype)
{txt}
{com}. ta state if racetype=="governor", summ( obs)

              {txt}{c |}       Summary of (count) obs
        state {c |}        Mean   Std. Dev.       Freq.
{hline 14}{c +}{hline 36}
      Alabama {c |}  {res}        15           0           1
{txt}       Alaska {c |}  {res}        11           0           1
{txt}      Arizona {c |}  {res}        18           0           1
{txt}     Arkansas {c |}  {res}        16           0           1
{txt}   California {c |}  {res}        16           0           1
{txt}     Colorado {c |}  {res}        15           0           1
{txt}  Connecticut {c |}  {res}        14           0           1
{txt}     Delaware {c |}  {res}        16           0           1
{txt}      Florida {c |}  {res}        16           0           1
{txt}      Georgia {c |}  {res}        15           0           1
{txt}       Hawaii {c |}  {res}         9           0           1
{txt}        Idaho {c |}  {res}        10           0           1
{txt}     Illinois {c |}  {res}        16           0           1
{txt}      Indiana {c |}  {res}        14           0           1
{txt}         Iowa {c |}  {res}        19           0           1
{txt}       Kansas {c |}  {res}        14           0           1
{txt}     Kentucky {c |}  {res}        12           0           1
{txt}    Louisiana {c |}  {res}        10           0           1
{txt}        Maine {c |}  {res}        13           0           1
{txt}     Maryland {c |}  {res}        15           0           1
{txt}Massachusetts {c |}  {res}        17           0           1
{txt}     Michigan {c |}  {res}        17           0           1
{txt}    Minnesota {c |}  {res}        15           0           1
{txt}  Mississippi {c |}  {res}        12           0           1
{txt}     Missouri {c |}  {res}        16           0           1
{txt}      Montana {c |}  {res}        16           0           1
{txt}     Nebraska {c |}  {res}        17           0           1
{txt}       Nevada {c |}  {res}        15           0           1
{txt}New Hampshire {c |}  {res}        31           0           1
{txt}   New Jersey {c |}  {res}        14           0           1
{txt}   New Mexico {c |}  {res}        17           0           1
{txt}     New York {c |}  {res}        11           0           1
{txt}North Carol.. {c |}  {res}        16           0           1
{txt} North Dakota {c |}  {res}        17           0           1
{txt}         Ohio {c |}  {res}        15           0           1
{txt}     Oklahoma {c |}  {res}        12           0           1
{txt}       Oregon {c |}  {res}        11           0           1
{txt} Pennsylvania {c |}  {res}        15           0           1
{txt} Rhode Island {c |}  {res}        23           0           1
{txt}South Carol.. {c |}  {res}        13           0           1
{txt} South Dakota {c |}  {res}        14           0           1
{txt}    Tennessee {c |}  {res}        12           0           1
{txt}        Texas {c |}  {res}        10           0           1
{txt}         Utah {c |}  {res}        17           0           1
{txt}      Vermont {c |}  {res}        28           0           1
{txt}     Virginia {c |}  {res}        15           0           1
{txt}   Washington {c |}  {res}        16           0           1
{txt}West Virginia {c |}  {res}        17           0           1
{txt}    Wisconsin {c |}  {res}        19           0           1
{txt}      Wyoming {c |}  {res}        12           0           1
{txt}{hline 14}{c +}{hline 36}
        Total {c |}  {res}     15.28   3.9899875          50
{txt}
{com}. ta state if racetype=="senator", summ( obs)

              {txt}{c |}       Summary of (count) obs
        state {c |}        Mean   Std. Dev.       Freq.
{hline 14}{c +}{hline 36}
      Alabama {c |}  {res}        20           0           1
{txt}       Alaska {c |}  {res}        21           0           1
{txt}      Arizona {c |}  {res}        21           0           1
{txt}     Arkansas {c |}  {res}        13           0           1
{txt}   California {c |}  {res}        21           0           1
{txt}     Colorado {c |}  {res}        21           0           1
{txt}  Connecticut {c |}  {res}        19           0           1
{txt}     Delaware {c |}  {res}        22           0           1
{txt}      Florida {c |}  {res}        18           0           1
{txt}      Georgia {c |}  {res}        21           0           1
{txt}       Hawaii {c |}  {res}        14           0           1
{txt}        Idaho {c |}  {res}        15           0           1
{txt}     Illinois {c |}  {res}        22           0           1
{txt}      Indiana {c |}  {res}        20           0           1
{txt}         Iowa {c |}  {res}        21           0           1
{txt}       Kansas {c |}  {res}        19           0           1
{txt}     Kentucky {c |}  {res}        17           0           1
{txt}    Louisiana {c |}  {res}        14           0           1
{txt}        Maine {c |}  {res}        19           0           1
{txt}     Maryland {c |}  {res}        20           0           1
{txt}Massachusetts {c |}  {res}        22           0           1
{txt}     Michigan {c |}  {res}        21           0           1
{txt}    Minnesota {c |}  {res}        22           0           1
{txt}  Mississippi {c |}  {res}        16           0           1
{txt}     Missouri {c |}  {res}        21           0           1
{txt}      Montana {c |}  {res}        21           0           1
{txt}     Nebraska {c |}  {res}        18           0           1
{txt}       Nevada {c |}  {res}        20           0           1
{txt}New Hampshire {c |}  {res}        21           0           1
{txt}   New Jersey {c |}  {res}        16           0           1
{txt}   New Mexico {c |}  {res}        20           0           1
{txt}     New York {c |}  {res}        15           0           1
{txt}North Carol.. {c |}  {res}        20           0           1
{txt} North Dakota {c |}  {res}        21           0           1
{txt}         Ohio {c |}  {res}        18           0           1
{txt}     Oklahoma {c |}  {res}        18           0           1
{txt}       Oregon {c |}  {res}        16           0           1
{txt} Pennsylvania {c |}  {res}        20           0           1
{txt} Rhode Island {c |}  {res}        21           0           1
{txt}South Carol.. {c |}  {res}        19           0           1
{txt} South Dakota {c |}  {res}        20           0           1
{txt}    Tennessee {c |}  {res}        13           0           1
{txt}        Texas {c |}  {res}        14           0           1
{txt}         Utah {c |}  {res}        19           0           1
{txt}      Vermont {c |}  {res}        16           0           1
{txt}     Virginia {c |}  {res}        18           0           1
{txt}   Washington {c |}  {res}        21           0           1
{txt}West Virginia {c |}  {res}        22           0           1
{txt}    Wisconsin {c |}  {res}        19           0           1
{txt}      Wyoming {c |}  {res}        13           0           1
{txt}{hline 14}{c +}{hline 36}
        Total {c |}  {res}     18.78   2.7202191          50
{txt}
{com}. 
. sort racetype state
{txt}
{com}. 
. //Probability of outcome at each margin
. // = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
. gen margin_1pp = 2*(.5-normal(-.0005/RShareSD)) //normal is cumulative so .5 minus this function is prob mass from limit to x=.5
{txt}
{com}. gen margin_01pp = 2*(.5-normal(-.00005/RShareSD))
{txt}
{com}. gen margin_001pp = 2*(.5-normal(-.000005/RShareSD))
{txt}
{com}. 
. label var margin_1pp "Pr(Margin<.1pp) (range 0,1)"
{txt}
{com}. label var margin_01pp "Pr(Margin<.01pp) (range 0,1)"
{txt}
{com}. label var margin_001pp "Pr(Margin<.001pp) (range 0,1)"
{txt}
{com}. 
. /*****************************************************
> Encode and order states
> *****************************************************/
. 
. levelsof state, local(statelist)
{res}{txt}`"Alabama"' `"Alaska"' `"Arizona"' `"Arkansas"' `"California"' `"Colorado"' `"Connecticut"' `"Delaware"' `"Florida"' `"Georgia"' `"Hawaii"' `"Idaho"' `"Illinois"' `"Indiana"' `"Iowa"' `"Kansas"' `"Kentucky"' `"Louisiana"' `"Maine"' `"Maryland"' `"Massachusetts"' `"Michigan"' `"Minnesota"' `"Mississippi"' `"Missouri"' `"Montana"' `"Nebraska"' `"Nevada"' `"New Hampshire"' `"New Jersey"' `"New Mexico"' `"New York"' `"North Carolina"' `"North Dakota"' `"Ohio"' `"Oklahoma"' `"Oregon"' `"Pennsylvania"' `"Rhode Island"' `"South Carolina"' `"South Dakota"' `"Tennessee"' `"Texas"' `"Utah"' `"Vermont"' `"Virginia"' `"Washington"' `"West Virginia"' `"Wisconsin"' `"Wyoming"'

{com}. 
. foreach statename in `statelist'{c -(}
{txt}  2{com}.         di "`statename'"
{txt}  3{com}. {c )-}
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
{txt}
{com}. 
. /*****************************************************
> Build Table as Matrix, export
> *****************************************************/
. 
. *Probability of an outcome within X% is a function of SD only
. matrix SENATE = J(52,6,.)
{txt}
{com}. levelsof state, local(statelist)
{res}{txt}`"Alabama"' `"Alaska"' `"Arizona"' `"Arkansas"' `"California"' `"Colorado"' `"Connecticut"' `"Delaware"' `"Florida"' `"Georgia"' `"Hawaii"' `"Idaho"' `"Illinois"' `"Indiana"' `"Iowa"' `"Kansas"' `"Kentucky"' `"Louisiana"' `"Maine"' `"Maryland"' `"Massachusetts"' `"Michigan"' `"Minnesota"' `"Mississippi"' `"Missouri"' `"Montana"' `"Nebraska"' `"Nevada"' `"New Hampshire"' `"New Jersey"' `"New Mexico"' `"New York"' `"North Carolina"' `"North Dakota"' `"Ohio"' `"Oklahoma"' `"Oregon"' `"Pennsylvania"' `"Rhode Island"' `"South Carolina"' `"South Dakota"' `"Tennessee"' `"Texas"' `"Utah"' `"Vermont"' `"Virginia"' `"Washington"' `"West Virginia"' `"Wisconsin"' `"Wyoming"'

{com}. local row = 2
{txt}
{com}. foreach statename in `statelist'{c -(}
{txt}  2{com}.         
.         qui summ RShareMean     if racetype=="senator" & state==`"`statename'"'
{txt}  3{com}.         matrix SENATE[`row',1]=r(mean)*100
{txt}  4{com}.         qui summ RShareSD               if racetype=="senator" & state==`"`statename'"'
{txt}  5{com}.         matrix SENATE[`row',2]=r(mean)*100
{txt}  6{com}.         
.         qui summ margin_1pp             if racetype=="senator" & state==`"`statename'"'
{txt}  7{com}.         matrix SENATE[`row',4]=r(mean)
{txt}  8{com}.         qui summ margin_01pp            if racetype=="senator" & state==`"`statename'"'
{txt}  9{com}.         matrix SENATE[`row',5]=r(mean)
{txt} 10{com}.         qui summ margin_001pp           if racetype=="senator" & state==`"`statename'"'
{txt} 11{com}.         matrix SENATE[`row',6]=r(mean)
{txt} 12{com}.         //Probability of outcome at each margin
.         // = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
.         
.         local row = `row'+1
{txt} 13{com}. {c )-}
{txt}
{com}. 
. matrix list SENATE
{res}
{txt}SENATE[52,6]
            c1         c2         c3         c4         c5         c6
 r1 {res}         .          .          .          .          .          .
{txt} r2 {res} 49.257129  20.879492          .  .00191069  .00019107  .00001911
{txt} r3 {res} 59.604269  17.430606          .  .00228874  .00022887  .00002289
{txt} r4 {res} 57.286066  13.493559          .  .00295653  .00029565  .00002957
{txt} r5 {res} 45.527601  25.714305          .  .00155144  .00015514  .00001551
{txt} r6 {res} 41.290334   14.94029          .  .00267024  .00026702   .0000267
{txt} r7 {res} 52.072132  6.4417027          .  .00619306  .00061931  .00006193
{txt} r8 {res} 41.760775  8.1380062          .  .00490218  .00049022  .00004902
{txt} r9 {res} 45.428067  9.7314306          .  .00409951  .00040995    .000041
{txt}r10 {res} 47.290021   11.15654          .  .00357585  .00035759  .00003576
{txt}r11 {res} 39.568093  20.462075          .  .00194966  .00019497   .0000195
{txt}r12 {res} 27.285072  8.2961403          .  .00480874  .00048088  .00004809
{txt}r13 {res} 62.411463  14.304577          .  .00278891  .00027889  .00002789
{txt}r14 {res} 44.434804  8.7336816          .  .00456783  .00045679  .00004568
{txt}r15 {res} 56.324011  13.574779          .  .00293884  .00029388  .00002939
{txt}r16 {res}  54.73063  10.021146          .  .00398099   .0003981  .00003981
{txt}r17 {res} 69.002569   13.16922          .  .00302935  .00030294  .00003029
{txt}r18 {res} 49.415252  10.334804          .  .00386017  .00038602   .0000386
{txt}r19 {res}  44.97591  19.533913          .   .0020423  .00020423  .00002042
{txt}r20 {res} 54.722404  15.195209          .  .00262544  .00026254  .00002625
{txt}r21 {res} 40.615299  9.7603634          .  .00408735  .00040874  .00004087
{txt}r22 {res} 38.449228  14.186651          .  .00281209  .00028121  .00002812
{txt}r23 {res} 44.225603  6.4813353          .  .00615519  .00061552  .00006155
{txt}r24 {res} 45.759013  8.0254972          .   .0049709  .00049709  .00004971
{txt}r25 {res} 62.403518  20.380735          .  .00195745  .00019574  .00001957
{txt}r26 {res} 51.032805  7.5833119          .  .00526075  .00052608  .00005261
{txt}r27 {res} 45.155141   9.103404          .  .00438232  .00043823  .00004382
{txt}r28 {res} 52.375418  14.149906          .  .00281939  .00028194  .00002819
{txt}r29 {res} 47.332713  7.1994945          .  .00554121  .00055413  .00005541
{txt}r30 {res} 55.020708  8.3417132          .  .00478247  .00047825  .00004782
{txt}r31 {res} 44.716129  4.9513936          .  .00805703  .00080572  .00008057
{txt}r32 {res} 49.590021  12.208935          .  .00326762  .00032676  .00003268
{txt}r33 {res} 39.376408  11.150107          .  .00357791  .00035779  .00003578
{txt}r34 {res} 49.496058  5.3217232          .  .00749638  .00074965  .00007496
{txt}r35 {res} 47.932836  15.304267          .  .00260673  .00026067  .00002607
{txt}r36 {res} 48.695454  10.074761          .   .0039598  .00039598   .0000396
{txt}r37 {res} 56.180078  16.099377          .  .00247799   .0002478  .00002478
{txt}r38 {res} 48.153564  9.8572262          .  .00404719  .00040472  .00004047
{txt}r39 {res} 52.759993    6.15266          .  .00648399  .00064841  .00006484
{txt}r40 {res} 38.170928  13.089579          .  .00304778  .00030478  .00003048
{txt}r41 {res} 53.185016  12.215149          .  .00326595   .0003266  .00003266
{txt}r42 {res}  54.80895  15.628526          .  .00255265  .00025527  .00002553
{txt}r43 {res} 56.927896  12.033617          .  .00331522  .00033152  .00003315
{txt}r44 {res} 56.421089  7.4363694          .   .0053647  .00053647  .00005365
{txt}r45 {res} 62.709928  9.6865192          .  .00411851  .00041185  .00004119
{txt}r46 {res} 51.368004  19.679569          .  .00202719  .00020272  .00002027
{txt}r47 {res} 50.495023  22.910427          .  .00174131  .00017413  .00001741
{txt}r48 {res} 41.584802  10.640812          .  .00374916  .00037492  .00003749
{txt}r49 {res} 37.170225  14.956287          .  .00266738  .00026674  .00002667
{txt}r50 {res} 42.518324  8.0941632          .  .00492873  .00049288  .00004929
{txt}r51 {res} 69.233334   9.088438          .  .00438954  .00043896   .0000439
{txt}r52 {res}         .          .          .          .          .          .
{reset}
{com}. 
. xml_tab SENATE, save("tables`c(dirsep)'TableA4a") replace


{txt}note: results saved to tables/TableA4a.xml

{com}. 
. 
. *Probability of an outcome within X% is a function of SD only
. matrix GOV = J(52,6,.)
{txt}
{com}. levelsof state, local(statelist)
{res}{txt}`"Alabama"' `"Alaska"' `"Arizona"' `"Arkansas"' `"California"' `"Colorado"' `"Connecticut"' `"Delaware"' `"Florida"' `"Georgia"' `"Hawaii"' `"Idaho"' `"Illinois"' `"Indiana"' `"Iowa"' `"Kansas"' `"Kentucky"' `"Louisiana"' `"Maine"' `"Maryland"' `"Massachusetts"' `"Michigan"' `"Minnesota"' `"Mississippi"' `"Missouri"' `"Montana"' `"Nebraska"' `"Nevada"' `"New Hampshire"' `"New Jersey"' `"New Mexico"' `"New York"' `"North Carolina"' `"North Dakota"' `"Ohio"' `"Oklahoma"' `"Oregon"' `"Pennsylvania"' `"Rhode Island"' `"South Carolina"' `"South Dakota"' `"Tennessee"' `"Texas"' `"Utah"' `"Vermont"' `"Virginia"' `"Washington"' `"West Virginia"' `"Wisconsin"' `"Wyoming"'

{com}. local row = 2
{txt}
{com}. foreach statename in `statelist'{c -(}
{txt}  2{com}.         
.         qui summ RShareMean     if racetype=="governor" & state==`"`statename'"'
{txt}  3{com}.         matrix GOV[`row',1]=r(mean)*100
{txt}  4{com}.         qui summ RShareSD               if racetype=="governor" & state==`"`statename'"'
{txt}  5{com}.         matrix GOV[`row',2]=r(mean)*100
{txt}  6{com}.         
.         qui summ margin_1pp             if racetype=="governor" & state==`"`statename'"'
{txt}  7{com}.         matrix GOV[`row',4]=r(mean)
{txt}  8{com}.         qui summ margin_01pp            if racetype=="governor" & state==`"`statename'"'
{txt}  9{com}.         matrix GOV[`row',5]=r(mean)
{txt} 10{com}.         qui summ margin_001pp           if racetype=="governor" & state==`"`statename'"'
{txt} 11{com}.         matrix GOV[`row',6]=r(mean)
{txt} 12{com}.         //Probability of outcome at each margin
.         // = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
.         
.         local row = `row'+1
{txt} 13{com}. {c )-}
{txt}
{com}. 
. matrix list GOV
{res}
{txt}GOV[52,6]
            c1         c2         c3         c4         c5         c6
 r1 {res}         .          .          .          .          .          .
{txt} r2 {res} 40.588671  20.687285          .  .00192844  .00019284  .00001928
{txt} r3 {res} 49.570024  9.0474561          .  .00440942  .00044094  .00004409
{txt} r4 {res} 51.639938  7.4287772          .  .00537019  .00053702   .0000537
{txt} r5 {res} 44.412109  12.524758          .  .00318522  .00031852  .00003185
{txt} r6 {res} 49.805567   8.118701          .  .00491384  .00049139  .00004914
{txt} r7 {res} 44.712535  11.353374          .  .00351385  .00035139  .00003514
{txt} r8 {res} 49.819604  7.6296434          .  .00522881  .00052288  .00005229
{txt} r9 {res} 46.439487  12.861758          .  .00310176  .00031018  .00003102
{txt}r10 {res} 47.707751  6.6588178          .  .00599113  .00059912  .00005991
{txt}r11 {res} 41.392216  15.948802          .  .00250139  .00025014  .00002501
{txt}r12 {res} 45.002496  9.0210557          .  .00442232  .00044223  .00004422
{txt}r13 {res} 55.027276  10.342564          .  .00385727  .00038573  .00003857
{txt}r14 {res} 53.638542  11.554804          .   .0034526  .00034526  .00003453
{txt}r15 {res} 52.368122  6.9968715          .  .00570168  .00057017  .00005702
{txt}r16 {res}  51.98769  7.3224582          .  .00544816  .00054482  .00005448
{txt}r17 {res} 51.350367  10.592949          .   .0037661  .00037661  .00003766
{txt}r18 {res} 42.333075  8.5402735          .  .00467128  .00046713  .00004671
{txt}r19 {res} 54.461038  15.504432          .  .00257308  .00025731  .00002573
{txt}r20 {res} 49.533397  7.1638249          .   .0055688  .00055688  .00005569
{txt}r21 {res} 42.494348  10.513806          .  .00379445  .00037945  .00003794
{txt}r22 {res} 50.848275  10.131951          .  .00393745  .00039375  .00003937
{txt}r23 {res} 51.599455  7.7654906          .  .00513734  .00051374  .00005137
{txt}r24 {res} 48.775652  7.4514396          .  .00535385  .00053539  .00005354
{txt}r25 {res} 51.151568  8.8969886          .  .00448399   .0004484  .00004484
{txt}r26 {res} 48.595262   7.998509          .  .00498768  .00049877  .00004988
{txt}r27 {res}  48.47298  11.428697          .   .0034907  .00034907  .00003491
{txt}r28 {res} 53.291297  13.002376          .  .00306822  .00030682  .00003068
{txt}r29 {res} 47.917396  15.883757          .  .00251163  .00025116  .00002512
{txt}r30 {res} 49.972573  11.108223          .   .0035914  .00035914  .00003591
{txt}r31 {res} 49.021855  10.058762          .   .0039661  .00039661  .00003966
{txt}r32 {res} 48.037523  6.5880105          .  .00605552  .00060556  .00006056
{txt}r33 {res}  46.50068   12.29194          .  .00324555  .00032456  .00003246
{txt}r34 {res} 46.848434  5.8852412          .  .00677861  .00067787  .00006779
{txt}r35 {res} 56.779426  12.668367          .  .00314911  .00031491  .00003149
{txt}r36 {res} 53.004974  10.272824          .  .00388346  .00038835  .00003883
{txt}r37 {res} 50.503469  9.7870134          .  .00407622  .00040762  .00004076
{txt}r38 {res} 47.097611  7.0969313          .  .00562129  .00056213  .00005621
{txt}r39 {res} 48.465335  7.9193205          .  .00503755  .00050376  .00005038
{txt}r40 {res} 45.825657  12.792705          .  .00311851  .00031185  .00003119
{txt}r41 {res} 50.676149  9.7049959          .  .00411067  .00041107  .00004111
{txt}r42 {res} 58.753467  7.7112459          .  .00517348  .00051735  .00005174
{txt}r43 {res} 54.066831  13.408186          .  .00297536  .00029754  .00002975
{txt}r44 {res} 53.705329  11.175394          .  .00356982  .00035698   .0000357
{txt}r45 {res} 56.884134  14.576936          .   .0027368  .00027368  .00002737
{txt}r46 {res} 50.210822   12.57588          .  .00317227  .00031723  .00003172
{txt}r47 {res} 52.936447  14.458705          .  .00275918  .00027592  .00002759
{txt}r48 {res} 47.874323   5.598902          .  .00712527  .00071254  .00007125
{txt}r49 {res} 46.011829  9.2707761          .   .0043032  .00043032  .00004303
{txt}r50 {res} 51.864111  6.1374862          .  .00650002  .00065001    .000065
{txt}r51 {res} 51.715833  14.576735          .  .00273684  .00027368  .00002737
{txt}r52 {res}         .          .          .          .          .          .
{reset}
{com}. 
. xml_tab GOV, save("tables`c(dirsep)'TableA4b") replace


{txt}note: results saved to tables/TableA4b.xml

{com}. 
. 
. 
. 
. 
. /*****************************************************
> OECD
> *****************************************************/
. 
. clear
{txt}
{com}. cd $directory
{res}/Users/mlg2296/Dropbox/recount/ReplicationPacket
{txt}
{com}. 
. 
. /*****************************************************
> Load and Reformat data
> *****************************************************/
. 
. use "source_data`c(dirsep)'OECD_election_results.dta"
{txt}
{com}. 
. /*****************************************************
> Sample Restriction
> *****************************************************/
. keep if year>=1988 & year<=2020
{txt}(1 observation deleted)

{com}. 
. //check sample size; check for missing data
. *xtset
. 
. /*****************************************************
> Make Vars
> *****************************************************/
. 
. egen group = group(polity)
{txt}
{com}. 
. //Find two party vote share
. gen Wshare = winner/(winner + loser)
{txt}
{com}. 
. sort group year
{txt}
{com}. 
. egen WShareSD = sd(Wshare), by(group)
{txt}
{com}. egen WShareMean = mean(Wshare), by(group)
{txt}
{com}. 
. gen obs = 1
{txt}
{com}. 
. collapse (first) WShareSD WShareMean (count) obs, by(polity)
{txt}
{com}. ta polity, summ( obs)

              {txt}{c |}       Summary of (count) obs
       polity {c |}        Mean   Std. Dev.       Freq.
{hline 14}{c +}{hline 36}
      Austria {c |}  {res}         5           0           1
{txt}        Chile {c |}  {res}         7           0           1
{txt}Czech Repub.. {c |}  {res}         2           0           1
{txt}      Finland {c |}  {res}         5           0           1
{txt}       France {c |}  {res}         6           0           1
{txt}      Iceland {c |}  {res}         6           0           1
{txt}      Ireland {c |}  {res}         4           0           1
{txt}    Lithuania {c |}  {res}         7           0           1
{txt}       Mexico {c |}  {res}         4           0           1
{txt}       Poland {c |}  {res}         7           0           1
{txt}     Portugal {c |}  {res}         6           0           1
{txt}     Slovakia {c |}  {res}         5           0           1
{txt}     Slovenia {c |}  {res}         7           0           1
{txt}  South Korea {c |}  {res}         6           0           1
{txt}       Turkey {c |}  {res}         2           0           1
{txt}{hline 14}{c +}{hline 36}
        Total {c |}  {res} 5.2666667   1.6676188          15
{txt}
{com}. 
. 
. //Probability of outcome at each margin
. // = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
. gen margin_1pp = 2*(.5-normal(-.0005/WShareSD)) //normal is cumulative so .5 minus this function is prob mass from limit to x=.5
{txt}
{com}. gen margin_01pp = 2*(.5-normal(-.00005/WShareSD))
{txt}
{com}. gen margin_001pp = 2*(.5-normal(-.000005/WShareSD))
{txt}
{com}. 
. label var margin_1pp "Pr(Margin<.1pp) (range 0,1)"
{txt}
{com}. label var margin_01pp "Pr(Margin<.01pp) (range 0,1)"
{txt}
{com}. label var margin_001pp "Pr(Margin<.001pp) (range 0,1)"
{txt}
{com}. 
. /*****************************************************
> Encode and order states
> *****************************************************/
. 
. levelsof polity, local(statelist)
{res}{txt}`"Austria"' `"Chile"' `"Czech Republic"' `"Finland"' `"France"' `"Iceland"' `"Ireland"' `"Lithuania"' `"Mexico"' `"Poland"' `"Portugal"' `"Slovakia"' `"Slovenia"' `"South Korea"' `"Turkey"'

{com}. 
. foreach statename in `statelist'{c -(}
{txt}  2{com}.         di "`statename'"
{txt}  3{com}. {c )-}
Austria
Chile
Czech Republic
Finland
France
Iceland
Ireland
Lithuania
Mexico
Poland
Portugal
Slovakia
Slovenia
South Korea
Turkey
{txt}
{com}. 
. /*****************************************************
> Build Table as Matrix, export
> *****************************************************/
. 
. *Probability of an outcome within X% is a function of SD only
. matrix C = J(52,6,.)
{txt}
{com}. levelsof polity, local(statelist)
{res}{txt}`"Austria"' `"Chile"' `"Czech Republic"' `"Finland"' `"France"' `"Iceland"' `"Ireland"' `"Lithuania"' `"Mexico"' `"Poland"' `"Portugal"' `"Slovakia"' `"Slovenia"' `"South Korea"' `"Turkey"'

{com}. local row = 2
{txt}
{com}. foreach statename in `statelist'{c -(}
{txt}  2{com}.         
.         qui summ WShareMean     if polity==`"`statename'"'
{txt}  3{com}.         matrix C[`row',1]=r(mean)*100
{txt}  4{com}.         qui summ WShareSD               if polity==`"`statename'"'
{txt}  5{com}.         matrix C[`row',2]=r(mean)*100
{txt}  6{com}.         
.         qui summ margin_1pp             if polity==`"`statename'"'
{txt}  7{com}.         matrix C[`row',4]=r(mean)
{txt}  8{com}.         qui summ margin_01pp            if polity==`"`statename'"'
{txt}  9{com}.         matrix C[`row',5]=r(mean)
{txt} 10{com}.         qui summ margin_001pp           if polity==`"`statename'"'
{txt} 11{com}.         matrix C[`row',6]=r(mean)
{txt} 12{com}.         //Probability of outcome at each margin
.         // = density underneath +/- .000005 for race within .001pp; ; +/- .00005 for race within .01pp; +/- .0005 for race within .1pp; 
.         
.         local row = `row'+1
{txt} 13{com}. {c )-}
{txt}
{com}. 
. matrix list C
{res}
{txt}C[52,6]
            c1         c2         c3         c4         c5         c6
 r1 {res}         .          .          .          .          .          .
{txt} r2 {res} 65.860832  15.847684          .  .00251735  .00025174  .00002517
{txt} r3 {res} 58.391804  7.5201944          .  .00530491  .00053049  .00005305
{txt} r4 {res} 53.086007  2.4269545          .  .01643682   .0016438  .00016438
{txt} r5 {res} 60.668063  13.515805          .  .00295167  .00029517  .00002952
{txt} r6 {res} 59.951961  12.149236          .  .00328367  .00032837  .00003284
{txt} r7 {res} 75.345933  17.715491          .  .00225194  .00022519  .00002252
{txt} r8 {res} 60.910314  7.4168243          .  .00537884  .00053789  .00005379
{txt} r9 {res} 61.415243  11.903957          .  .00335133  .00033513  .00003351
{txt}r10 {res} 57.424814  8.9138255          .  .00447552  .00044755  .00004476
{txt}r11 {res} 58.759022  11.133713          .  .00358318  .00035832  .00003583
{txt}r12 {res} 68.647468  10.036641          .  .00397484  .00039749  .00003975
{txt}r13 {res} 58.083665  1.7663665          .  .02258246  .00225854  .00022585
{txt}r14 {res}  64.83292  8.9136988          .  .00447559  .00044756  .00004476
{txt}r15 {res} 56.244659  6.2981062          .  .00633425  .00063343  .00006334
{txt}r16 {res} 60.291964  4.0948447          .  .00974231  .00097425  .00009743
{txt}r17 {res}         .          .          .          .          .          .
{txt}r18 {res}         .          .          .          .          .          .
{txt}r19 {res}         .          .          .          .          .          .
{txt}r20 {res}         .          .          .          .          .          .
{txt}r21 {res}         .          .          .          .          .          .
{txt}r22 {res}         .          .          .          .          .          .
{txt}r23 {res}         .          .          .          .          .          .
{txt}r24 {res}         .          .          .          .          .          .
{txt}r25 {res}         .          .          .          .          .          .
{txt}r26 {res}         .          .          .          .          .          .
{txt}r27 {res}         .          .          .          .          .          .
{txt}r28 {res}         .          .          .          .          .          .
{txt}r29 {res}         .          .          .          .          .          .
{txt}r30 {res}         .          .          .          .          .          .
{txt}r31 {res}         .          .          .          .          .          .
{txt}r32 {res}         .          .          .          .          .          .
{txt}r33 {res}         .          .          .          .          .          .
{txt}r34 {res}         .          .          .          .          .          .
{txt}r35 {res}         .          .          .          .          .          .
{txt}r36 {res}         .          .          .          .          .          .
{txt}r37 {res}         .          .          .          .          .          .
{txt}r38 {res}         .          .          .          .          .          .
{txt}r39 {res}         .          .          .          .          .          .
{txt}r40 {res}         .          .          .          .          .          .
{txt}r41 {res}         .          .          .          .          .          .
{txt}r42 {res}         .          .          .          .          .          .
{txt}r43 {res}         .          .          .          .          .          .
{txt}r44 {res}         .          .          .          .          .          .
{txt}r45 {res}         .          .          .          .          .          .
{txt}r46 {res}         .          .          .          .          .          .
{txt}r47 {res}         .          .          .          .          .          .
{txt}r48 {res}         .          .          .          .          .          .
{txt}r49 {res}         .          .          .          .          .          .
{txt}r50 {res}         .          .          .          .          .          .
{txt}r51 {res}         .          .          .          .          .          .
{txt}r52 {res}         .          .          .          .          .          .
{reset}
{com}. 
. xml_tab C, save("tables`c(dirsep)'TableA5") replace


{txt}note: results saved to tables/TableA5.xml

{com}. 
. 
. 
. 
{txt}end of do-file

{com}. 
. 
. 
. 
. cap log close
{smcl}
{com}{sf}{ul off}